I'm using RoboSpice with GsonSpringAndroidSpiceService. I also want to add Realm to save the data.
the problem is that in realm each object has to extend realmObject, but the gson in the roboSpice trying to parse the realmObject instead of ignore it.
I've tried to add exclusion stategy:
Gson gson = new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getDeclaringClass().equals(RealmObject.class); } @Override public boolean shouldSkipClass(Class<?> clazz) { return false; } }) .create();
then
GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter(gson);
also tried (separately) to add @expose to the fields in the object, and
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
in the two ways I have the same error:
09-07 11:21:04.615 8443-8443/com.mm.rsApp D/OpenGLRenderer﹕ Enabling debug mode 0 09-07 11:21:04.620 8443-8443/com.mm.rsApp D//SpiceManager.java:1018﹕ 11:21:04.625 main Bound to service : GsonSpiceService 09-07 11:21:04.625 8443-8466/com.mm.rsApp D//SpiceManager.java:1133﹕ 11:21:04.628 Thread-16362 Bound ok. 09-07 11:21:04.690 8443-8443/com.mm.rsApp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41b8f088 time:525639153 09-07 11:21:21.890 8443-8466/com.mm.rsApp D//SpiceManager.java:245﹕ 11:21:21.896 Thread-16362 Sending request to service : CachedSpiceRequest 09-07 11:21:21.895 8443-8466/com.mm.rsApp D//RequestProcessor.java:70﹕ 11:21:21.898 Thread-16362 Adding request to queue 1103121632: CachedSpiceRequest [requestCacheKey=login, cacheDuration=-1, spiceRequest=com.mm.rsApp.requests.BaseRequest@41d700c0] size is 0 09-07 11:21:21.900 8443-8466/com.mm.rsApp D//RequestProcessor.java:89﹕ 11:21:21.902 Thread-16362 Adding entry for type class com.mm.rsApp.model.LoginResult and cacheKey login. 09-07 11:21:21.900 8443-8466/com.mm.rsApp D//RequestProgressManager.java:54﹕ 11:21:21.904 Thread-16362 Request was found when adding request listeners to existing requests. 09-07 11:21:21.900 8443-8466/com.mm.rsApp D//RequestProgressManager.java:64﹕ 11:21:21.905 Thread-16362 Sending progress PENDING 09-07 11:21:21.905 8443-8609/com.mm.rsApp D//RequestRunner.java:68﹕ 11:21:21.908 Thread-16369 Processing request : CachedSpiceRequest [requestCacheKey=login, cacheDuration=-1, spiceRequest=com.mm.rsApp.requests.BaseRequest@41d700c0] 09-07 11:21:21.905 8443-8609/com.mm.rsApp D//RequestRunner.java:114﹕ 11:21:21.910 Thread-16369 Cache content not available or expired or disabled 09-07 11:21:21.910 8443-8609/com.mm.rsApp D//RequestRunner.java:128﹕ 11:21:21.913 Thread-16369 Calling netwok request. 09-07 11:21:21.910 8443-8609/com.mm.rsApp D//RequestProgressManager.java:64﹕ 11:21:21.914 Thread-16369 Sending progress LOADING_FROM_NETWORK 09-07 11:21:21.915 8443-8443/com.mm.rsApp V//DefaultRequestProgressReporter.java:110﹕ 11:21:21.917 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@41d71138 09-07 11:21:21.915 8443-8443/com.mm.rsApp V//DefaultRequestProgressReporter.java:110﹕ 11:21:21.921 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@41d83c50 09-07 11:21:27.255 8443-8609/com.mm.rsApp D//RequestRunner.java:131﹕ 11:21:27.257 Thread-16369 Network request call ended. 09-07 11:21:27.255 8443-8609/com.mm.rsApp D//RequestRunner.java:151﹕ 11:21:27.259 Thread-16369 Start caching content... 09-07 11:21:27.260 8443-8609/com.mm.rsApp D//RequestProgressManager.java:64﹕ 11:21:27.262 Thread-16369 Sending progress WRITING_TO_CACHE 09-07 11:21:27.260 8443-8443/com.mm.rsApp V//DefaultRequestProgressReporter.java:110﹕ 11:21:27.265 main Notifying 1 listeners of progress com.octo.android.robospice.request.listener.RequestProgress@41e12c08 09-07 11:21:27.440 8443-8609/com.mm.rsApp I/dalvikvm﹕ threadid=18: stack overflow on call to Lcom/google/gson/internal/$Gson$Types;.resolve:LLLL 09-07 11:21:27.440 8443-8609/com.mm.rsApp I/dalvikvm﹕ method requires 96+20+12=128 bytes, fp is 0x5deea348 (72 left) 09-07 11:21:27.440 8443-8609/com.mm.rsApp I/dalvikvm﹕ expanding stack end (0x5deea300 to 0x5deea000) 09-07 11:21:27.440 8443-8609/com.mm.rsApp I/dalvikvm﹕ Shrank stack (to 0x5deea300, curFrame is 0x5deeac94) 09-07 11:21:27.480 8443-8609/com.mm.rsApp D//RequestRunner.java:199﹕ 11:21:27.482 Thread-16369 An unexpected error occurred when processsing request CachedSpiceRequest [requestCacheKey=login, cacheDuration=-1, spiceRequest=com.mm.rsApp.requests.BaseRequest@41d700c0] java.lang.StackOverflowError at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:141) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.Gson.getAdapter(Gson.java:359) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:92) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.Gson.getAdapter(Gson.java:359) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:92) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.Gson.getAdapter(Gson.java:359) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:92) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.Gson.getAda
please help me with any idea why I get this error and how can I solve it
I used Robospice
with Retrofit
and Realm
.
I solved it by overriding createConverter()
in my Service
class that extends RetrofitGsonSpiceService
. Unfortunately, I can't say more clearer because I haven't used GsonSpringAndroidSpiceService
.
So in my Service
class it looked like this:
@Override
protected Converter createConverter() {
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
return new GsonConverter(gson);
}
So the idea is to find where Gson
can be initialized with default configuration. And that's why I passed the above mentioned configuration to Retrofit
also.
new RestAdapter.Builder()
.setConverter(new GsonConverter(gson))
.build();
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With