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.
You can use a similar block for asynchronous calls: Call call = client. newCall(request);\ call. enqueue(new Callback() {\ public void onResponse(Call call, Response response) throws IOException {\ try (ResponseBody responseBody = response.
First, we create the OkHttpClient instance. Then, we need to create the request via the Request object and its Builder class. Next step is to pass the request in parameter of the newcall method of the OkHttpClient instance created. OkHttp supports asynchronous and synchronous calls.
OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.
As of Android 5.0, OkHttp is part of the Android platform and is used for all HTTP calls.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With