Mohanish Nerurkar
Mohanish Nerurkar

Reputation: 551

Retrofit 2.0.0-beta4: Unable to create converter for class

Here are my files:

build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.6.1'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
    compile 'com.android.databinding:baseLibrary:2.0.0-beta2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
}

Pojo Model class:

@Table(name = "ModelTableName")
public class DataEntity extends Model {

    @Column(name = "_id")
    private String _id;

    @Expose
    @Column(name = "imageUri")
    private String uri;

    @Expose
    @Column(name = "title")
    private String title;

    public DataEntity() {
        super();
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String get_id() {
        return _id;
    }

    public String getUri() {
        return uri;
    }

    public String getTitle() {
        return title;
    }
}

Retrofit service:

protected Retrofit build() {
        if (null == retrofit) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

I am using retrofit for API calls and for database using ActiveAndroid. On Pre 6.0 Android devices, an app is working perfectly. Not able to identify the root cause.

Gone through the following post:

Retrofit 2 beta-4. Android 6. Unable to create convertor

I couldn't understand that what is the need to use gson or gsonbuilder ?

Any help appreciated.

Upvotes: 2

Views: 2519

Answers (1)

Rohan Arora
Rohan Arora

Reputation: 661

They have now released a stable version of Retrofit. Try using this

compile 'com.squareup.retrofit2:retrofit:2.0.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.0'

Also if you are using ActiveAndroid then you should create your retrofit object like this

Retrofit retrofit = new new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(
                    GsonConverterFactory.create(new GsonBuilder()
                            .serializeNulls()
                            .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
                            .create()))
            .build();

because Model class contains some static and transient fields. Hope this helps.

Upvotes: 3

Related Questions