Gson StackOverflow错误Android

时间:2016-09-23 13:16:01

标签: android gson

我尝试StackOverflow Serialize JSON String POJO Object Constructors时有两个public class ObjectClass extends BaseObservable implements Serializable { @Inject Realm realm; private String TAG = "ObjectClass"; private String name; private String address; public ObjectClass() { } public ObjectClass(Context context) { ((MainApplication) context.getApplicationContext()).getMainAppComponent().inject(this); } public String getName() { return name; } public void setname(String name) { this.name = name; } @Bindable public String getAddress() { return address; } public void setaddress(String address) { this.address = address; Realmaddress result = realm.where(Realmaddress.class).equalTo("id", address).findFirst(); if (result != null) Log.e(TAG, result.toString()); } }

我收到public ObjectClass(Context context) { ((MainApplication) context.getApplicationContext()).getMainAppComponent().inject(this); } 错误
String testJson = "{\n" +
                "\t\"name\" : \"Test Name\",\n" +
                "\t\"address\" : \"Test Address\"\n" +
                "}"

Gson gson = new GsonBuilder().setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'").setPrettyPrinting().create();
    ObjectClass objectClass = gson.fromJson(testJson, ObjectClass.class);

我应该删除此构造函数

StackOverflow
一切正常。但是,如果我离开它,我会遇到StackOverFlow错误。

@Inject Realm realm;

一切都只是挂起,过了一会儿,我收到了@Expose(deserialize = false, serialize = false) @Inject Realm realm;错误

编辑1

所以,经过深度调试。我注意到构造函数不是问题所在。这是GSON Exclusion Strategy因此,我尝试执行此操作public class CustomGsonParser { public static Gson returnCustomParser(){ return new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { if (f.getAnnotation(Inject.class) != null || f.getDeclaringClass().equals(RealmObject.class) || f.getDeclaringClass().equals(Realm.class)) { Log.e("GSONBuild", "Exists Inject"); return true; } return false; } @Override public boolean shouldSkipClass(Class<?> clazz) { if (clazz.equals(Inject.class)) return true; return false; } }) .create(); } } ,但也没有修复它。

编辑2

我编辑了df1 chr start end name 1 12334 12334 AAA 1 2342 2342 SAP 2 3456 3456 SOS 3 4537 4537 ABR df2 chr start end name 1 12334 12334 DSF 1 3421 3421 KSF 2 7689 7689 LUF df3 chr start end name 1 12334 12334 DSF 1 3421 3421 KSF 2 4537 4537 LUF 3 8976 8976 BAR 4 6789 6789 AIN 看起来像这样。但是,即使它进入if块并记录它存在

,它也无法修复它
chr start   end     name    Sample
1   12334   12334   AAA df1
1   12334   12334   AAA df2
1   12334   12334   AAA df3

2 个答案:

答案 0 :(得分:0)

当您使用import os import sys import pip import re pip.main(['install', 'robotframework==3.0']) pip.main(['install', 'robotframework-selenium2library==1.8.0']) # Checksums can be looked up by chromedriver version here - http://chromedriver.storage.googleapis.com/index.html pip.main(['install', '--upgrade', 'chromedriver_installer', '--install-option=--chromedriver-version=2.24', '--install-option=--chromedriver-checksums=1a46c83926f891d502427df10b4646b9,d117b66fac514344eaf80691ae9a4687,' + 'c56e41bdc769ad2c31225b8495fc1a93,8e6b6d358f1b919a0d1369f90d61e1a4']) #Add the Scripts dir to the path, since that's where the chromedriver is installed scriptsDir = re.sub('[A-Za-z0-9\\.]+$', '', sys.executable) + 'Scripts' os.environ['PATH'] += os.pathsep + scriptsDir 时,以下行中的某些内容无法正确获取dependency injection并且无法进入serialised,因此您获得了{{1}错误,因为你的记忆在某段时间内变得疲惫不堪。

serialisation

如果您的代码中无法使用上述句子或以其他方式执行此操作,请将其删除。

答案 1 :(得分:0)

所以,我注意到Realm实际上是主要原因。我还是不知道为什么。所以,我所做的是传递了上下文而不再dependency inject Model。我使用上下文来创建Realm实例。所以,我的Object Class现在看起来像这样

public class ObjectClass extends BaseObservable implements Serializable {

    private String TAG = "ObjectClass";

    private String name;
    private String address;
    @Expose(deserialize = false, serialize = false)
    private Context context;

    public ObjectClass() {
    }

    public ObjectClass(Context context) {
        this.context = context;
    }

    public String getName() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    @Bindable
    public String getAddress() {
        return address;
    }

    public void setaddress(String address) {
        this.address = address;
        Realmaddress result = RealmUtils.getInstance(context).where(Realmaddress.class).equalTo("id", address).findFirst();
        if (result != null)
            Log.e(TAG, result.toString());
    }
}