简体   繁体   English

改造:GSON StackOverflow

[英]Retrofit : gson stackoverflow

I try to make API requests with Retrofit and gson library , I followed this tutorial to make my API request with retrofit : http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/ but I encounter an error. 我尝试使用Retrofit和gson库发出API请求,我按照本教程进行了Retrofit发出API请求: http ://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/遇到错误。 This is different parts of log message : 这是日志消息的不同部分:

Retrofit﹕ java.lang.StackOverflowError at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:372)
...

 API.ItemTypeAdapterFactory.create(ItemTypeAdapterFactory.java:20)
        at com.google.gson.Gson.getAdapter(Gson.java:359)

This is the class ItemTypeAdapterFactory: 这是ItemTypeAdapterFactory类:

public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

    return new TypeAdapter<T>() {

        public void write(JsonWriter out, T value) throws IOException {
            delegate.write(out, value);
        }

        public T read(JsonReader in) throws IOException {
            JsonElement jsonElement = elementAdapter.read(in);
            if (jsonElement.isJsonObject()) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                if (jsonObject.has("content") && jsonObject.get("content").isJsonObject())
                {
                    jsonElement = jsonObject.get("content");
                }
            }
            return delegate.fromJsonTree(jsonElement);
        }
    }.nullSafe();
}
}

JSON JSON

{
    "http_code":200,
    "content":{
        "token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "expires":"2015-03-18T04:45:53.066Z",
        "client":"XXXXXXXXXXXXXXXXXXXX",
        "createdAt":"2015-02-16T04:45:53.066Z",
        "updatedAt":"2015-02-16T04:45:53.066Z",
        "id":"XXXXXXXXXXXXXXXXXXXXX"
    }
}

So I made a request, everything is ok, I get the right answer and I can get values but after the request this bug appear and I have no idea why, is it an issue from Gson library ? 因此,我提出了一个请求,一切正常,我得到了正确的答案,我可以获取值,但是在请求出现之后,这个bug出现了,我不知道为什么,这是Gson库的问题吗? There is a way to fix that ? 有办法解决吗?

I try it on Nexus 5 android 5.0.1, retrofit 1.9.0 and gson 2.3.1 我在Nexus 5 android 5.0.1,翻新版1.9.0和gson 2.3.1上尝试过

Thank you in advance for any help ! 预先感谢您的任何帮助 !

Error happens when Gson tries to resolve type information about an object it have to deserialize. 当Gson尝试解析有关必须反序列化的对象的类型信息时,会发生错误。 It gets into an infinite recursion due to the cyclic reference to your Book class in extends declaration. 由于扩展声明中对Book类的循环引用,它进入了无限递归。

However even if Gson could cope with your class, I would not recommend using these libraries combination. 但是,即使Gson可以应付您的课程,我也不建议使用这些库组合。 This happens when there are compatibility issues. 出现兼容性问题时,会发生这种情况。 Try using older version of Gson and remove retrofit dependency from the project. 尝试使用版本的Gson,并从项目中删除改造依赖项。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM