Android, how to get a response string from a callback using OkHttp?

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 save the value of response.body (). string () from the onResponse () method in the responseString variable, but I cannot access it.

+6
source share
3 answers

I think you want to do 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 exercise, we implement a callback, and then when we create the okhttp request, we pass it an instance of our (this), and so we can get oktthp to call the class, we could make the inner class just as easy, but it reduces the number of classes we need to do. I used the button to illustrate when the Http call was made, but it can happen another time, for example, it can happen when the screen is first created (in onCreate). Be careful when turning the screen. It also assumes that the callback is executed in the main thread, which I think will be, but I'm not sure, since I use okttp differently than you do. If it does not return the results of the response in the main thread, you can call runOnUiThread () and pass Runnable to it, which does the job of updating the views.

+4
source

If you move the responseString declaration as an instance variable, you can assign its value in the onResponse method of your Callback .

 public class MyClass { private String responseString; // your class implementation } 

I changed 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); 
+2
source

The first answer is close, but you cannot assign a final variable in the onResponse method. The workaround is to enter final String[] responseString = new String[1]; and assignment responseString[0] = response.body().string();

+1
source

Source: https://habr.com/ru/post/990040/


All Articles