0

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. 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:

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

{
    "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 ? 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

Thank you in advance for any help !

Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42
flo3573358
  • 7
  • 1
  • 7
  • Can you post the Json – Tushar Gogna Feb 16 '15 at 05:19
  • The Json is fine. Must be something wrong with Gson. This happens when there are compatibility issues. Try using [this](https://code.google.com/p/google-gson/downloads/detail?name=google-gson-1.7.1-release.zip&can=2&q=) version of Gson. – Tushar Gogna Feb 16 '15 at 05:34
  • Ok I download your version, I have some problems when I try to build my projects , let me fix this , I will keep you in contact when everything will be fine – flo3573358 Feb 16 '15 at 05:52
  • Alright, let me know if you need some assistance. – Tushar Gogna Feb 16 '15 at 06:25
  • Problem is retrofit has dependencies and use gson 2.3.1, when I try to compile gson 1.7.1 there is a conflict when I build project and the error is : `com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer;` My dependencies are : compile 'com.android.support:support-v4:21.0.0' compile 'com.google.android.gms:play-services:6.1.71' compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0' compile 'com.squareup.okhttp:okhttp:2.2.0' compile 'com.squareup.retrofit:retrofit:1.9.0' compile files('libs/gson-1.7.1.jar') Should I exclude gson module of retrofit ? – flo3573358 Feb 16 '15 at 06:36
  • This is a build path issue. See the solution [here](http://stackoverflow.com/a/8071713/3531756). – Tushar Gogna Feb 16 '15 at 06:39
  • I check it , everything is fine , I think it's a conflict between retrofit and gson library because there is two different version, like retrofit use gson 2.3.1 – flo3573358 Feb 16 '15 at 06:58
  • Use one of them, not both. – Tushar Gogna Feb 16 '15 at 07:09
  • Ok I succeed to remove dependencies for retrofit and so on , error disappear now , thank you very much – flo3573358 Feb 16 '15 at 12:49

1 Answers1

5

Error happens when Gson tries to resolve type information about an object it have to deserialize. It gets into an infinite recursion due to the cyclic reference to your Book class in extends declaration.

However even if Gson could cope with your class, I would not recommend using these libraries combination. This happens when there are compatibility issues. Try using older version of Gson and remove retrofit dependency from the project.

Hope it helps!

Tushar Gogna
  • 4,963
  • 6
  • 31
  • 42