GSON.fromJson stackoverflow错误

时间:2014-04-22 05:19:36

标签: android gson stack-overflow

我试图在JellyBean设备上加载我的Json到我的类时遇到异常 stackoverflow错误,但在ICS上它完全没问题,这是一个错误吗?

JSON

{
  "user": {

     "id": 216,
     "city_id": 30910,
     "city_name": "Edinburgh",
     "country_id": 482,
     "country_name": "United Kingdom",
     "email": "q@q.com",
     "username": "q@q.com",
     "name": "Alex",
     "surname": "Qwerty",
     "gender": "male",
     "send_emails": 1,
     "birthday": 969483600,
     "interests": "burns, wiskey, beer",
     "categories": [
         {
            "id": 11,
            "name": "Art",
            "icon": "http://my_icon.png",
            "subcategories": [
                {
                    "id": 39,
                    "name": "Graphic"
                }
            ]
        }
    ]
}

}

我的班级

public class User implements Serializable, Cloneable{
public final static String MALE = "male";
public final static String FEMALE = "female";
public int id;
public int city_id;
public String city_name="";
public int country_id;
public String country_name="";
public String email="";
public String username="";
public String name="";
public String surname="";
public String gender="";
public int send_emails;
public Long birthday;
public String interests="";
public int fb_id;
public int vk_id;
public String plain_password="";

public ArrayList<Category> categories;

@Override
public User clone() throws CloneNotSupportedException {
    return (User)super.clone();
  }
}

public class Category {
public int id;
public String name;
public String icon;
public BitmapDrawable iconBitmap;
public ArrayList<SubCategory> subcategories;
public int checkedSubCategoriesCount;
public int changedSubCategoriesCount;

public boolean equalCategory(Category mCategory) {
    return id == mCategory.id;
}
}

public class SubCategory {
public int id;
public String name;
public boolean isChecked;
public boolean isChanged;

public boolean equalCategory(SubCategory mCategory) {
    return id == mCategory.id;
}
 }

方式

public void getCurrentUser(final AuthToken token, final LoadCompleteListener listener) {
    if (listener != null) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    URL url = new  URL(mURL_API.concat(SysUtilHelper.getDeviceLanguage() + "/user?token=" + token.token));
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestProperty("Connection", "close");
                    conn.connect();
                    if (getResponseCode(conn) == HttpURLConnection.HTTP_OK) {
                        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                        JsonObject request = (JsonObject) mJparser.parse(in.readLine());
                        in.close();
                        if (!request.has("error")) {

                            User user = mGson.fromJson(request.getAsJsonObject("user"), new TypeToken<User>() {
                            }.getType());
                            listener.onLoadSucces(user);
                        } else {
                            listener.onLoadError(request.get("message").toString());
                        }
                        conn.disconnect();
                    } else {
                        int responseCode = conn.getResponseCode();
                        conn.disconnect();
                        throw new NetworkException(ERROR_BAD_REQUEST + responseCode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    listener.onLoadError(e.toString());
                }

            }
        }).start();

    }
}

P.S。我正在使用gson 2.2.4

更新

这是我的logcat

04-22 01:37:12.415: E/AndroidRuntime(807): FATAL EXCEPTION: Thread-79
04-22 01:37:12.415: E/AndroidRuntime(807): java.lang.StackOverflowError
04-22 01:37:12.415: E/AndroidRuntime(807):  at    com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:371)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:375)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:380)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.$Gson$Types.resolve($Gson$Types.java:355)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTyp eAdapterFactory.java:117)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.Gson.getAdapter(Gson.java:356)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.Gson.getAdapter(Gson.java:356)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.Gson.getAdapter(Gson.java:356)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
04-22 01:37:12.415: E/AndroidRuntime(807):  at com.google.gsonfixed.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeA

1 个答案:

答案 0 :(得分:0)

我认为BufferedReader中的问题

 {
        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is,"UTF8");
        BufferedReader br=new BufferedReader(isr);
        String read;
        StringBuffer sb = new StringBuffer();
        while((read = br.readLine()) != null) {
            sb.append(read);
        }
        br.close();
        sb.toString();

}

还尝试使用fromJson(JsonObject,User.class)函数