Making an HTTP request using the OkHttp library inside doInBackground () in AsyncTask blocks the user interface

I tried to make an OkHttp request in AsyncTask using call.execute () - Synchronous call.

I have two buttons in my layout. Pressing button1 starts AsyncTask, which executes OkHttp request.call.execute ().

And by pressing button 2, I just update the TextView.

Observation . While AsyncTask is working, I cannot update the TextView.

But if I do not use AsyncTask and use the OkHttpClient.newCall (). Queque () method, then I can update the TextView by pressing button2.

Any answer "Why using AsyncTask in this case does not work"?

Source code example:

bpost = (Button) findViewById(R.id.bpost);
    bpost.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            i++;
            tv.setText(""+i);
        }
    });


bstart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        OkHttpHandler handler = new OkHttpHandler();

            byte[] image = new byte[0];
            try {
                image = handler.execute(url).get();
                if (image != null && image.length > 0) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
                    imageView.setImageBitmap(bitmap);
                    tv.setText("Total btytes download: " + image.length);
                }
            } catch (Exception e) {
                tv.setText("sorry, something went wrong!");
            }
        }


public class OkHttpHandler extends AsyncTask<String, Void, byte[]> {

    OkHttpClient client = new OkHttpClient();

    @Override
    protected byte[] doInBackground(String... params) {

        Request.Builder builder = new Request.Builder();
        builder.url(params[0]);
        Request request = builder.build();



        try {
            Response response = client.newCall(request).execute();
            return response.body().bytes();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
+4
1

, get() AsyncTask doInBackground, . . .

UIThread , doInBackground - (.. CancellationException, ExecutionException InterruptedException).

onPostExecute(Result) AsyncTask.

private class OkHttpHandler extends AsyncTask<String, Void, byte[]> {

        OkHttpClient client = new OkHttpClient();

        @Override
        protected byte[] doInBackground(String... params) {

            Request.Builder builder = new Request.Builder();
            builder.url(params[0]);
            Request request = builder.build();
            try {
                Response response = client.newCall(request).execute();
                return response.body().bytes();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(byte[] bytes) {
            super.onPostExecute(bytes);
            try {
                if (bytes != null && bytes.length > 0) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    imageView.setImageBitmap(bitmap);
                    tv.setText("Total btytes download: " + bytes.length);
                }
            } catch (Exception e) {
                tv.setText("sorry, something went wrong!");
            }
        }
    }
+9

All Articles