当我遇到here所讨论的使用okhttpclient
导致内存泄漏的问题时,我将改造更新为最新版本com.squareup.retrofit2:retrofit:2.5.0
,同时我尝试使用responseBody.close(); inside try{} catch(){}
块关闭主体,如下所示:这样here的指示
appSettingService.getAppSetting().enqueue(new Callback<AppSettingModel.AppSettingMetaData>() {
@Override
public void onResponse(Call<AppSettingModel.AppSettingMetaData> call, Response<AppSettingModel.AppSettingMetaData> response) {
try (ResponseBody responseBody = response.body()) {
Log.e("res code appSetting", String.valueOf(response.code()));
if (response.isSuccessful()) {
List<AppSettingModel> results = response.body().getValue();
if (results != null && results.size() > 0) {
saveToDatabase(results.get(0));
}
}
responseBody.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<AppSettingModel.AppSettingMetaData> call, Throwable t) {
}
});
但是我在较低版本的Android中遇到了这个问题,因为在Api 16
上运行时应用崩溃,并且显示了此错误
java.lang.NoClassDefFoundError: javax/annotation/Nullable
at java.lang.reflect.Field.getAnnotation(Native Method)
at java.lang.reflect.Field.getAnnotation(Field.java:212)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldNames(ReflectiveTypeAdapterFactory.java:72)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
at com.google.gson.Gson.getAdapter(Gson.java:423)
at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:64)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:328)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:311)
at retrofit2.HttpServiceMethod.createResponseConverter(HttpServiceMethod.java:68)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:46)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:36)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at $Proxy0.getAppSetting(Native Method)