OkHTTP Update UI from Queue Callback

I am trying to use the OkHTTP library. When calling the server and receiving a successful response. I need to update the interface.

How can this be done when making an Async call using the enqueue () parameter?

client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { // NEED TO UPDATE UI HERE } } }); 
+7
android
source share
4 answers

You can refer to the following code sample, hoping this helps!

 public class MainActivity extends AppCompatActivity { private static final String LOG_TAG = "OkHttp"; private TextView mTextView; private Handler mHandler; private String mMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.textView); mHandler = new Handler(Looper.getMainLooper()); OkHttpClient client = new OkHttpClient(); // GET request Request request = new Request.Builder() .url("http://...") .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { mMessage = e.toString(); Log.e(LOG_TAG, mMessage); // no need inside run() mHandler.post(new Runnable() { @Override public void run() { mTextView.setText(mMessage); // must be inside run() } }); } @Override public void onResponse(Response response) throws IOException { mMessage = response.toString(); Log.i(LOG_TAG, mMessage); // no need inside run() mHandler.post(new Runnable() { @Override public void run() { mTextView.setText(mMessage); // must be inside run() } }); } }); } } 
+7
source

Try the following:

 Handler mainHandler = new Handler(Looper.getMainLooper()); mainHandler.post(new Runnable() { @Override public void run() { // code to interact with UI } }); 
+5
source

If your code does not update the user interface, I would suggest you specify a stream, since the user interface is in its own stream:

 client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { runOnUiThread(new Runnable() { @Override public void run() { //TODO: update your UI } }); } } }); 
+5
source

In order to have clean code, I suggest you not put all your code in runnable.

A simple example:

 public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://...").build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { final String body = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { updateUI(body); } }); } }); } private void updateUI(responseBody){ //TODO: update your UI } } 
0
source

All Articles