【问题标题】:Retrofit Freeze the Ui改造冻结用户界面
【发布时间】:2018-10-23 15:22:35
【问题描述】:

我正在使用 AsyncTask 将我的位图转换为 base64 字符串。 之后在 asyncTask 的后期执行中,我尝试使用改造将该 base64 字符串上传到服务器。

但是当我提出改造请求时,我的应用程序会冻结。

这是我的代码:

  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();
    }
}

这是我的改造请求代码:

 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());
        }
    });

请帮帮我

【问题讨论】:

  • 调用enqueue之后有没有调用任务? Retrofit 已经在后台运行,因此它不会阻止您的 UI。我想它一定是别的东西
  • 这是因为你的请求格式不对。就我而言,我试图通过从本地 SQLIte DB 而不是 Java 对象获取 Realm 对象来发送它。 Retrofit 只将 Java 对象转换为 JSON,而不是 Realm 对象。请确保在使用 Retrofit 时发送正确的 JSON 作为请求。
  • 我也遇到过类似的问题,请查看stackoverflow.com/a/55195053/2462531 我是如何修复的。

标签: android android-asynctask retrofit freeze


【解决方案1】:

您仍在执行后调用改造...当您与 ui 元素交互时,这是在主线程上。通过调用改造,它会成为线程的瓶颈,直到发送 base64 字符串。

【讨论】:

    【解决方案2】:

    Retrofit 默认在后台运行,并使用回调将结果发送到主线程。查看分析器,然后您可以了解哪一侧占用了您设备的更多内存。

    【讨论】:

      【解决方案3】:

      在我的情况下,someString.equalsIgnoreCase(otherString) 在someString 为空时停止了剩余的代码执行(没有错误);

      【讨论】:

        【解决方案4】:

        在我的情况下,Retrofit 的创建/初始化是在您使用 Moshi 等适配器时特别阻塞 UI 线程的一种。其他人也在他们的仓库中报告了这个problem。

        【讨论】:

          猜你喜欢
          • 2016-07-22
          • 2017-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-08
          • 1970-01-01
          • 2021-09-02
          • 1970-01-01
          相关资源
          最近更新 更多