反序列化Android Retrofit时Gson stackOverflown

时间:2016-02-12 12:10:11

标签: java android gson retrofit2

我正在使用retrofit2和Gson结合使用API​​ Rest点。 我有一个CarOption模型:

public class CarOption {

private String              name;
private String              label;
private boolean             isActive;
private String              type;
private Integer             onglet;
private Integer             position;
private List<CarOptionData> data;
private View                view;

和CarForm模型:

public class CarForm {

// DEBUG
private static final String TAG = CarForm.class.getName();

private ArrayList<CarOption>        fields = new ArrayList<>();
private static CarOptionComparator  coc = new CarOptionComparator();

private static ArrayList<CarOption> tab0 = null;
private static ArrayList<CarOption> tab1 = null;
private static ArrayList<CarOption> tab2 = null;
private static ArrayList<CarOption> tab3 = null;

public static void downloadCarForm(final Context context, final IRequestResponseListener cb) {
    Retrofit retrofit = RetrofitSingleton.getInstance();

    IWebServiceApi apiService = retrofit.create(IWebServiceApi.class);
    Call<CarForm>   call = apiService.getCarForm(User.getAccessToken());
    call.enqueue(new Callback<CarForm>() {
        @Override
        public void onResponse(Response<CarForm> response, Retrofit retrofit) {
            int statusCode = response.code();
            if (statusCode == 200 || statusCode == 201) {
                CarForm carForm = response.body(); // STACKOVERFLOWN ERROR FROM GSON
                carForm.unCrashOnglet(carForm.fields);
                Collections.sort(carForm.fields, coc);
                CarForm.setTab0(createTab(context, carForm.getFields(), 0));
                CarForm.setTab1(createTab(context, carForm.getFields(), 1));
                CarForm.setTab2(createTab(context, carForm.getFields(), 2));
                CarForm.setTab3(createTab(context, carForm.getFields(), 3));
                if (cb != null)
                    cb.onSuccess();

当Gson尝试反序列化我的对象时,我有一个stackoverflown错误。我读到了由父对象引用引起的可能错误,但我没有。无法看到错误的来源。 它可能链接到“tab”arrayList属性,因为它在我创建它们之前工作。

这是错误:

W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:334)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
 W/System.err:     at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:358)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:155)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:97)
 W/System.err:     at com.google.gson.Gson.getAdapter(Gson.java:407)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:136)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:49)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:106)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:105)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:161)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:97)
 W/System.err:     at com.google.gson.Gson.getAdapter(Gson.java:407)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:136)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:49)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:106)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:105)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:161)
 W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:97)
 W/System.err:     at com.google.gson.Gson.getAdapter(Gson.java:407)

你对这个问题有什么看法吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

如评论所述:

public class CarOption {
...
private View                view;

成了:

public class CarOption {
...
private Object                view;

解决了我的问题。然后,当我需要访问它时,我将其转换回View。