Retrofit: Retrofit 2 conflicts with ActiveAndroid ORM on Marshmallow

Created on 30 Mar 2016  ·  9Comments  ·  Source: square/retrofit

I am using Retrofit 2 with ActiveAndroid ORM

This is Retrofit library that I am using _compile 'com.squareup.retrofit2:retrofit:2.0.0'_

Here is my POJO

`@Table(name = "Authentication")
public class Auth extends com.activeandroid.Model {

@Column(name = "token_type")
@SerializedName("token_type")
@Expose
private String tokenType;

@Column(name = "access_token")
@SerializedName("access_token")
@Expose
private String accessToken;

@Column(name = "expires_in")
@SerializedName("expires_in")
@Expose
private Integer expiresIn;
}`

This error comes up on Marshmallow below Marshmallow its working fine.

`E/AndroidRuntime: FATAL EXCEPTION: main

                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{test.retrofit20example/test.retrofit20example.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for class test.retrofit20example.pojo.Auth
                                                                                 for method Authorization.getToken
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: java.lang.IllegalArgumentException: Unable to create converter for class test.retrofit20example.pojo.Auth
                                                                                 for method Authorization.getToken
                                                                                 at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:667)
                                                                                 at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:653)
                                                                                 at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:166)
                                                                                 at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
                                                                                 at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
                                                                                 at java.lang.reflect.Proxy.invoke(Proxy.java:393)
                                                                                 at $Proxy5.getToken(Unknown Source)
                                                                                 at test.retrofit20example.MainActivity.onCreate(MainActivity.java:48)
                                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                    ... 9 more
                                                                              Caused by: java.lang.SecurityException: Can't make field constructor accessible
                                                                                 at java.lang.reflect.Constructor.setAccessible(Constructor.java:334)
                                                                                 at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:101)
                                                                                 at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:83)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:95)
                                                                                 at com.google.gson.Gson.getAdapter(Gson.java:416)
                                                                                 at com.google.gson.internal.bind.MapTypeAdapterFactory.getKeyAdapter(MapTypeAdapterFactory.java:140)
                                                                                 at com.google.gson.internal.bind.MapTypeAdapterFactory.create(MapTypeAdapterFactory.java:125)
                                                                                 at com.google.gson.Gson.getAdapter(Gson.java:416)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
                                                                                 at com.google.gson.Gson.getAdapter(Gson.java:416)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:135)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:105)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:104)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:160)
                                                                                 at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
                                                                                 at com.google.gson.Gson.getAdapter(Gson.java:416)
                                                                                 at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:63)
                                                                                at retrofit2.Retrofit.n`

If I do not extend Auth class to Model class of ActiveAndroid It workd fine on Marshmallow but I want to use ActiveAndroid as well

Can you please tell what should I do?

Most helpful comment

@HenryBrightChao Hope this sample code will help you

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .client(getClient()) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create())) .build();

All 9 comments

This is a problem with Gson being unable to create instances of your Auth type, probably due to some fields on the base class. You need to consult the ActiveAndroid people for this as its their type which is causing the problem when used with Gson.

Your words help me. Thanks!
I found solution. I just need to add this excludeFieldsWithoutExposeAnnotation()

@zeeshanaslam78 I have the problem,too.
How do you resolve it?
excludeFieldsWithoutExposeAnnotation,i don't exactly know how to use?

@HenryBrightChao Hope this sample code will help you

Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constants.BASE_URL) .client(getClient()) .addConverterFactory(GsonConverterFactory.create(new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create())) .build();

@zeeshanaslam78
well done.Thanks.

@HenryBrightChao 您好,我想请教下关于retrofit跟activeAndroid冲突的问题,怎么联系您。

@TimAimee 你好,我的qq 3214749428. ActiveAndroid早就不维护了,我这边尝试了一下,就换成了 GreenDao,效率提高了不少.

@zeeshanaslam78
thank

@zeeshanaslam78 THANKS +10

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zheangrybear picture zheangrybear  ·  3Comments

colintheshots picture colintheshots  ·  3Comments

kkunsue picture kkunsue  ·  3Comments

MetaiR picture MetaiR  ·  3Comments

Ne1c picture Ne1c  ·  3Comments