10

我正在为 android 开发一个应用程序,它使用 JSON 字符串从服务器下载兴趣点。一切正常,但自从我开始在 4.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); <---

和我正在尝试创建的课程:

/**
 * 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);
    }
}

请注意我知道我在这里使用的糟糕的设计模式,这个应用程序是为我在大学学习的移动应用程序课程,所以放轻松!

编辑 原来将 gson 降级到 1.7.1 对我有用。

4

2 回答 2

14

事实证明,正如 Erik Nedwidek 指出的那样,这是一个错误。我采取了简单的方法并将 gson 从2.2.2降级到1.7.1,现在一切都很好!

于 2013-01-28T17:15:40.823 回答
3

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

评论 #12:

我相信是在 API 17 中,WeakReference 和 SoftReference 的内部更改为自引用,这会触发这种情况。你是在使用 Gson 来序列化 WeakReference 还是 SoftReference?如果是,您应该为这些类型编写自己的 TypeAdapter。

您的代码中的某些内容导致了循环引用,并且由于它一直工作到 4.2.1 (API 17),它很可能是 Java 类之一。它仍然可能是您的代码,但我非常怀疑。

于 2013-01-28T15:57:16.390 回答