(Retrofit) Could not locate converter for class crashing app

JavaAndroidRetrofit

Java Problem Overview


So Retrofit 2.0.0 was recently released and theres not really any updated examples on how to use it, but im trying to implement it for a basic API call. Im getting a

java.lang.IllegalArgumentException: Unable to create converter for class` 

caused by

Caused by: java.lang.IllegalArgumentException: Could not locate converter for class orbyt.app.dataclass. Tried:
* retrofit.OkHttpBodyConverterFactory

When trying to make the api call.

Java Solutions


Solution 1 - Java

I was facing the same issue. I fixed it by adding :

compile 'com.squareup.retrofit2:converter-gson:<latest-version>'

to my build.gradle

Then specify the converter when creating my Retrofit instance.

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Solution 2 - Java

In Retrofit 2.0, Converter is not included in the package and when you are using Retrofit 2.0 Make Sure follow new URL pattern

Base URL: always ends with /

@Url: DO NOT start with /

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(Constants.API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

For more information about 2.0 Follow this link Retrofit 2.0: The biggest update

And also update build.gradle.

implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

And add the extension in project level build.gradle file

ext {
retrofit_version= "2.x.x"
}

Solution 3 - Java

Change retrofit version accordingly

For me below dependency was there already

compile 'com.squareup.retrofit2:retrofit:2.0.2'

For gson 2.0.2 I changed

compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Then add

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Solution 4 - Java

For Retrofit V2 add the following repositories -

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

Now use below code -

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

Hope it will help :)

Solution 5 - Java

In the latest Retrofit 2.0,you should import the latest version :

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

Be careful call baseUrl(),at v2.0,it should be end of "/",and at the method ,you would’t start thr url with"/"

@POST("classes/info")
Call<ContactBean> insertInfo(@Body ContactBean bean);

And you can see Retrofit to get more info! Hope help !

Solution 6 - Java

In my case (Kotlin with coroutines) I received the exception:

> Unable to create converter for retrofit2.Call

> for method Queries.exportPdf. > > Caused by: > java.lang.IllegalArgumentException: Could not locate ResponseBody > converter for retrofit2.Call

A problem was in a request:

@FormUrlEncoded
@Streaming
@POST("export-pdf/")
suspend fun exportPdf(
    @Field("token") token: String
): Call<ResponseBody>

Removed suspend from definition and exceptions disappeared.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOrbitView Question on Stackoverflow
Solution 1 - JavaYacSrkView Answer on Stackoverflow
Solution 2 - JavaAjit Kumar DubeyView Answer on Stackoverflow
Solution 3 - JavaAbilash RajasekaranView Answer on Stackoverflow
Solution 4 - JavaNeoView Answer on Stackoverflow
Solution 5 - JavajoeView Answer on Stackoverflow
Solution 6 - JavaCoolMindView Answer on Stackoverflow