Reputation: 79
This is my code:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
}
};
okHttpClient.newCall(request).enqueue(callback);
String responseString;
In the above code, I want to store the value of response.body().string() from the onResponse() method in the variable responseString, however I can't access it.
Upvotes: 8
Views: 15083
Reputation: 9294
I think what you want to do is something like this:
public class MyActivity extends Activity implements Callback , View.OnClickListener {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
findViewById(R.id.DoHttp).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId(() == R.id.DoHttp) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
okHttpClient.newCall(request).enqueue(this);
}
}
@Override
public void onFailure(Request request, IOException e) {
//do something to indicate error
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
parse(response.body().string());
}
}
private void parse(String response) {
//do something with response
}
}
In the above activity we implement Callback and then when we create the okhttp request, we pass it an instance of ourself (this) and that way we can get oktthp to call back the class, we could have done an inner class just as easily but this reduces the # of classes we have to make. I used a button click to illustrate when the Http call is made but that could be some other time, for instance it could happen when the screen is first created (in onCreate). Be careful though of screen rotations. Also this assumes that the callback is done on the main thread which I think it would be but I'm not positive as I use okttp in a different way than you. If it does not return the results on the response on the main thread then you can call runOnUiThread() and pass it a Runnable that does the work of updating the views.
Upvotes: 6
Reputation: 3470
If you move the responseString
declaration to be an instance variable then you will be able to assign its value in the onResponse
method of your Callback
.
public class MyClass {
private String responseString;
// your class implementation
}
I have modified the code you posted with the necessary changes below:
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
Callback callback = new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
responseString = response.body().string();
}
};
okHttpClient.newCall(request).enqueue(callback);
Upvotes: 2
Reputation: 31
First answer is close, but you cannot assign final variable in method onResponse. Workaround is to type final String[] responseString = new String[1];
and assign responseString[0] = response.body().string();
Upvotes: 1