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 !