我正在尝试通过Retrofit发出一个简单的REST请求,并且遇到很多GC错误.我真的不知道该如何解决.我试图在模拟器上放更多的内存,但问题仍然存在.当进行同步呼叫Response< List< User>响应= usersCall.execute();
以下例外
java.lang.RuntimeException: An error occurred while executing doInBackground()
android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Cause by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:831)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:422)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:410)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
并使用pojo模型Response< List< User>响应= usersCall.execute();然后
04-09 07:37:23.897 13396-13411/? I/art: Clamp target GC heap from 111MB to 96MB
04-09 07:37:23.897 13396-13411/? I/art: Background partial concurrent mark sweep GC freed 3104(48KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 7.520ms total 194.127ms
04-09 07:37:23.897 13396-13396/? I/art: WaitForGcToComplete blocked for 182.909ms for cause Alloc
04-09 07:37:23.929 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 128(5KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 8.237ms total 31.761ms
04-09 07:37:23.929 13396-13396/? I/art: WaitForGcToComplete blocked for 18.171ms for cause Alloc
04-09 07:37:24.130 13396-13411/? I/art: Clamp target GC heap from 111MB to 96MB
04-09 07:37:24.130 13396-13411/? I/art: Background partial concurrent mark sweep GC freed 5784(90KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 9.388ms total 200.412ms
04-09 07:37:24.130 13396-13396/? I/art: WaitForGcToComplete blocked for 200.758ms for cause Alloc
04-09 07:37:24.158 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 1710(34KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.897ms total 27.286ms
04-09 07:37:24.158 13396-13396/? I/art: WaitForGcToComplete blocked for 18.297ms for cause Alloc
04-09 07:37:24.346 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
04-09 07:37:24.346 13396-13396/? I/art: Alloc partial concurrent mark sweep GC freed 2092(33KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 5.943ms total 188.191ms
04-09 07:37:24.370 13396-13396/? I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 8.501ms total 23.440ms
04-09 07:37:24.566 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
04-09 07:37:24.566 13396-13396/? I/art: Alloc concurrent mark sweep GC freed 9(12KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.105ms total 195.296ms
04-09 07:37:24.566 13396-13411/? I/art: WaitForGcToComplete blocked for 395.241ms for cause Background
04-09 07:37:24.598 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.138ms total 29.195ms
04-09 07:37:24.598 13396-13396/? I/art: WaitForGcToComplete blocked for 29.063ms for cause Alloc
04-09 07:37:24.775 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
04-09 07:37:24.775 13396-13396/? I/art: Alloc partial concurrent mark sweep GC freed 16(56KB) AllocSpace objects, 1(54KB) LOS objects, 0% free, 95MB/96MB, paused 6.803ms total 177.429ms
我的代码:
Gson gson =
new GsonBuilder()
.registerTypeAdapter(Usuario.class, new UsuarioDeserializer())
.registerTypeAdapter(Date.class, new CustomDateDeserializer())
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ConstantesUtil.URL_BASE)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
UsuariosAPI usuarioApi = retrofit.create(UsuariosAPI.class);
Usuario usuario = new Usuario(login, senha);
Call<List<Usuario>> usuarios = usuarioApi.getUsuarios(usuario);
usuarios.enqueue(new Callback<List<Usuario>>() {
@Override
public void onResponse(Call<List<Usuario>> call, Response<List<Usuario>> response) {
List<Usuario> usuarios = response.body();
for (Usuario u: usuarios) {
Log.i(TAG, u.getNome());
}
}
@Override
public void onFailure(Call<List<Usuario>> call, Throwable t) {
}
});
解决方法:
我意识到当模型(在您的情况下为“ Usuario”)具有导致序列化问题的成员变量时,会发生此问题(无限GC循环).
public class Usuario {
@SerializedName("id")
public String id;
// the following will cause infinite loop of GC
public TextView problemView;
// use transient keyword will solve the problem in GC infinite loop
private transient TextView okView;
}