Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHTTP Update UI from enqueue callback

Tags:

android

okhttp

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 an Async call using the enqueue()?

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

        } 
    }
});
like image 413
TareK Khoury Avatar asked Oct 29 '15 15:10

TareK Khoury


4 Answers

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()
                    }
                });
            }
        });
    }
}
like image 196
BNK Avatar answered Oct 20 '22 07:10

BNK


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
    }

}
like image 40
modu Avatar answered Sep 22 '22 18:09

modu


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
                }

            });
        } 
    }
});
like image 7
Iliiaz Akhmedov Avatar answered Oct 20 '22 07:10

Iliiaz Akhmedov


Try this:

Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
    @Override
    public void run() {
        // code to interact with UI
    }
});
like image 5
Vitaly Zinchenko Avatar answered Oct 20 '22 05:10

Vitaly Zinchenko