Gson 2.2.2只在4.2.1上产生一个stackoverflow

我正在开发一个应用程序的Android使用JSONstring下载服务器感兴趣的点。 一切工作正常,但由于我已经开始testing4.2.1,我得到了以下错误:

01-28 15:32:14.167: E/AndroidRuntime(31174): FATAL EXCEPTION: AsyncTask #1 01-28 15:32:14.167: E/AndroidRuntime(31174): java.lang.RuntimeException: An error occured while executing doInBackground() 01-28 15:32:14.167: E/AndroidRuntime(31174): at android.os.AsyncTask$3.done(AsyncTask.java:299) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 01-28 15:32:14.167: E/AndroidRuntime(31174): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 01-28 15:32:14.167: E/AndroidRuntime(31174): at java.lang.Thread.run(Thread.java:856) 01-28 15:32:14.167: E/AndroidRuntime(31174): Caused by: java.lang.StackOverflowError 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:371) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353) 01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java: 

奇怪的是,它在我尝试过的每个其他版本上工作正常…

这里是我认为这是崩溃的线:

  Gson g = new Gson(); JSONResponse jsonResponse = g.fromJson(serverReply, JSONResponse.class); <--- 

和我正在创build的class级:

 /** * The JSONResponse from the server * @author Tom * */ public class JSONResponse { private String status; private PointOfInterest result; /** * Creates a JSONResponse object * @param json * @return */ static JSONResponse convertJSONToResponse(String json){ Gson gson = new Gson(); JSONResponse jsonResponse = gson.fromJson(json, JSONResponse.class); return jsonResponse; } /** * Get Point of Interest */ public PointOfInterest getPointofInterest(){ return result; } public String getStatus(){ return status; } /** * Get PrivateSection, area we can put our own data */ /* public String getPrivateSection(){ return privateSection; } */ /** * Creates a JSONResponse object * @param json * @return */ String convertJSONResponseToJSON(){ Gson gson = new Gson(); return gson.toJson(this); } } 

请注意我知道我在这里雇用的可怕的devise模式,这个应用程序是针对我在大学读书的移动应用程序课程,所以请放轻松点!

编辑原来,降级gson为1.7.1适用于我。

Solutions Collecting From Web of "Gson 2.2.2只在4.2.1上产生一个stackoverflow"

所以事实certificate,这是埃里克Nedwidek指出的一个错误。 我把轻松的路线从2.2.2降级到了1.7.1 ,现在一切都好起来!

看看这个错误报告。 http://code.google.com/p/google-gson/issues/detail?id=440

评论#12:

我相信在API 17中,WeakReference和SoftReference的内部变成了自我指涉,这将触发这一点。 你使用Gson来序列化一个WeakReference或SoftReference? 如果你是,你应该为这些types编写自己的TypeAdapter。

代码中的某些内容导致了循环引用,并且由于它在4.2.1(API 17)之前运行,所以它很可能是Java类之一。 它仍然可以是你的代码,但我非常怀疑。