Retrofit Freeze the Ui

android retrofit synchronous call example
how to handle anr in android
anr android
retrofit callback on background thread
multiple api call in retrofit android
how retrofit works
retrofit post example
retrofit get request

i am using AsyncTask to convert my bitmap to base64 string. After that in the post execute of asyncTask i am trying to upload that base64 string into the server using retrofit.

But my app get freeze when i make the retrofit request.

Here is my code:

  public class ConvertBitmapToString extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        String base64String = FrequentFunctions.convertBitmapToBase64(singleBitmapDetail.getBitMap());
        return base64String;
    }


    @Override
    protected void onPostExecute(final String result) {
        hideProgressBar();
        hitInsertImageApi(result);
    }

    @Override
    protected void onPreExecute() {
        baseShowProgressBar();
    }
}

Here is my code for retrofit requesst:

 Call<InsertImageResponse> callback = api.insertUpdateImage(userId, imageid, imageData);
    callback.enqueue(new Callback<InsertImageResponse>() {
        @Override
        public void onResponse(Call<InsertImageResponse> call, Response<InsertImageResponse> response) {
            if (response.body().getReturnMessage().equalsIgnoreCase("success")) {
                handler.onSuccess(response.body());
            } else {
                handler.onError("Something went wrong");
            }
        }

        @Override
        public void onFailure(Call<InsertImageResponse> call, Throwable t) {
            handler.onError(t.getMessage());
        }
    });

Please help me out


Your still calling retrofit in post execute... this is on the main thread at this point when you interact with ui elements. By calling retrofit it is bottlenecking the thread till the base64 string gets sent.

Gson parsing freezes UI · Issue #1067 · square/retrofit · GitHub, We just started to use some services in our project with big data results. First thing we've noticed is that the UI freezes when response come. Currently, the UserRepositories is a synchronous method, which will freeze the UI and crash the app for Android 4.0 or later. As a rule, network requests from the UI thread need to happen asynchronously.


Retrofit by default run on background and send the result to main thread using callback. See profiler, then you can understand which side is taking the more memory of your device.

RETROFIT FREEZES THE UI WHILE WAITING FOR RESPONSE , u/themavencoder1y. RETROFIT FREEZES THE UI WHILE WAITING FOR RESPONSE. Library. [removed]. Share3. 0. 3 Comments sorted byBest. Log in or sign  In retrofit, synchronous methods are executed on the main thread. This means the UI is blocked during the request execution and no user interaction is possible in this period. UserService service = ServiceGenerator.createService(UserService.class); // 1.


In my case someString.equalsIgnoreCase(otherString) stopped remaining code execution (no error) when someString was null;

How to make HTTP calls on Android with Retrofit 2, Today, I'll look at using the Retrofit 2 HTTP client to see how the Android framework won't allow you to perform network calls on the UI thread. Retrofit— A simple Android tutorial. Since this request is asynchronous, Retrofit handles it on a background thread so that the main UI thread isn't blocked or interfered with.


Keeping your app responsive, This keeps the UI thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen. Retrofit — Synchronous and Asynchronous Requests. Within the previously published tutorials, we walked you through the setup of Retrofit.This tutorial shows you how to perform the actual requests in either a synchronous or a asynchronous way.


Retrofit freezes the UI while waiting for the response, It works just fine most of the time, but when I use it to send pictures (as Blobs) with a post, retrofit freezes the UI thread waiting for the onResponse. After 30-40  Select the UI freeze event by clicking on the corresponding bar in the process overview section of the Threads tool window. This will apply the filter by the UI freeze event. Note that this applies not only the filter by the freeze time range, but also the filter by the Main thread. The latter is done automatically, as the Main thread is the only one that processes the UI interaction in our application.


Retrofit - A REST Client for Android (Retrofit 2.X), Developing an Android application using Retrofit to connect to an API. which will freeze the UI and crash the app for Android 4.0 or later. Image upload to server using retrofit in android part -3 In this tutorial Series we are going to learn about how to upload an image to web server using retrofit from an android application. Like