OkHTTP Update UI from enqueue callback

前端 未结 4 1948
醉话见心
醉话见心 2020-12-31 09:41

I am trying to use OkHTTP library. When making a call to the server and getting a successful response back. i need to update the UI.

How can this be done when doing

相关标签:
4条回答
  • 2020-12-31 10:20

    If your code doesn't update the UI, I would suggest you to specify the thread, since UI is on its own thread:

    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
                    }
    
                });
            } 
        }
    });
    
    0 讨论(0)
  • 2020-12-31 10:21

    You can refer to the following sample code, hope 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()
                        }
                    });
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-31 10:25

    Try this:

    Handler mainHandler = new Handler(Looper.getMainLooper());
    mainHandler.post(new Runnable() {
        @Override
        public void run() {
            // code to interact with UI
        }
    });
    
    0 讨论(0)
  • 2020-12-31 10:38

    For the sake of having a clean code, I would suggest you not to put your entire code inside the 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 讨论(0)
提交回复
热议问题