1

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

Deepak Rana
  • 539
  • 4
  • 18
  • Do you call any task after calling `enqueue`? `Retrofit` already run in the background so it cannot block your UI. I assume it must be something else – Tam Huynh Oct 24 '18 at 10:37
  • This is because your request format is wrong. In my case, I am trying to send a Realm object by getting it from local SQLIte DB instead of Java object. Retrofit converts only Java object to JSON but not Realm object. Please make sure you are sending a right JSON as request when using Retrofit. – Shailendra Madda Mar 16 '19 at 08:36
  • I too faced similar issue checkout this https://stackoverflow.com/a/55195053/2462531 how I fixed. – Shailendra Madda Mar 16 '19 at 08:57

4 Answers4

0

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.

Vahalaru
  • 380
  • 4
  • 15
0

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.

Shaon
  • 2,496
  • 26
  • 27
0

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

user5480949
  • 1,410
  • 1
  • 15
  • 22
0

In my case the creation/initialization of Retrofit is the one that blocking UI thread specially when you are working with adapters like Moshi. Others also reported this problem at their repo.

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67