25

I'm making a simple RetroFit application for educational purposes, and use IntelliJ IDEA as my IDE.

I have imported the Retrofit library properly (at least I think I have) but I'm not able to get the Gson Converter package. I have the gson.jar from google installed but nowhere in either of these libraries is there a class called "GsonConverterFactory", which is required for me to parse JSON.

Edit: I'm on Windows.

manoj
  • 3,391
  • 2
  • 20
  • 30
vic.vele
  • 391
  • 1
  • 3
  • 13

4 Answers4

22

Add compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' to your build.gradle file and get the dependency resolved or add corresponding jars to your bulid path.

then use GsonConverterFactory.create() to get the Converter Factory

I tried using 2.0.0-beta1 but it gave me an illegal type conversion error for factory as given below, so moved to 2.0.0-beta2

  error: method addConverterFactory in class Builder cannot be applied to   given types;
    required: Factory
    found: GsonConverterFactory
    reason: actual argument GsonConverterFactory cannot be converted to Factory by method invocation conversion

So my suggestion is to use 2.0.0-beta2

my build.gradle has following dependencies to resolve retrofit.

 compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'  
 compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
manoj
  • 3,391
  • 2
  • 20
  • 30
20

If you are using retrofit 2, you need to include the convert-gson package. For gradle builds, you can add compile 'com.squareup.retrofit:converter-gson:2.0.0-beta3' to your dependencies section.

For other build systems, or to download the jar, checkout the Maven Central convert-gson page.

iagreen
  • 31,470
  • 8
  • 76
  • 90
15

in your Module:app build.gradle add

compile 'com.squareup.retrofit2:converter-gson:[retrofit2 version]'

the version above is same with your retrofit2 version, so for example your retrofit2 version is 2.1.0, than your build.gradle should be like this :

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Faruk
  • 5,438
  • 3
  • 30
  • 46
14

Try this

/* JSON */
compile 'com.google.code.gson:gson:2.5'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

// >Retrofit & OkHttp
compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.1'
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Thiago
  • 12,778
  • 14
  • 93
  • 110