【发布时间】: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