90

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.

Zombo
  • 1
  • 62
  • 391
  • 407
Orbit
  • 2,985
  • 9
  • 49
  • 106

7 Answers7

149

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();
YacSrk
  • 1,555
  • 1
  • 11
  • 9
  • 2
    So i fixed this last night actually by basically copying [these](https://github.com/square/retrofit/tree/master/retrofit-converters/gson/src/main/java/retrofit) 2 classes into my project and calling the `.addConverterFactory(GsonConverterFactory.create())`. Linking the converters via gradle is obviously a cleaner option, so marking this as the answer. – Orbit Sep 02 '15 at 20:42
  • 1
    Also make sure that your version for `converter-gson` matches your version for `retrofit` itself - see http://stackoverflow.com/questions/32902157/retrofit-convertor-factory-can-not-access-gsonconverterfactory – gravitron Nov 06 '15 at 17:27
  • This answer is out of date, Gradle won't even sync with this version, much less compile. You need compile 'com.squareup.retrofit2:converter-gson:2.0.2' – Andrew Koster May 13 '16 at 03:24
18

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"
}
Ajit Kumar Dubey
  • 1,383
  • 1
  • 19
  • 33
7

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();
  • This answer is up to date, it should have more votes than the outdated one and more than the one that doesn't even include the relevant dependency. – Andrew Koster May 13 '16 at 03:26
6

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 :)

Neo
  • 3,546
  • 1
  • 24
  • 31
3

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 !

joe
  • 619
  • 6
  • 10
2

I know that it's old question. But I've faced the same problem. And no one answer suits to me.
According to the documentation, it ships with support for OkHttp's RequestBody and ResponseBody types.
So no need to include .addConverterFactory(AnyFactory.create()) in your builder. There is an opportunity works with raw type of a response. All you need is to return Response<ResponseBody> or Call<ResponseBody> or Single<Response<ResponseBody>> from an interface of api.

ilyamuromets
  • 403
  • 1
  • 7
  • 18
  • if you didn't add addConverterFactory retrofit response always in ResponseBody. And if you add addConverterFactory( gson or other ) then you must provide model class in response. ConverterFactory is convert json or your date in to model so model must needed – Rahul Mandaliya Jan 09 '23 at 13:20
1

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.

CoolMind
  • 26,736
  • 15
  • 188
  • 224