2015-09-06 7 views
18

私は教育目的で単純なRetroFitアプリケーションを作成しており、IntelliJ IDEAをIDEとして使用しています。RetroFitでGsonコンバータを使用するにはどうすればよいですか?

私はRetrofitライブラリを適切にインポートしましたが(少なくとも私は持っていると思いますが)、Gson Converterパッケージを入手することはできません。私はgson.jarがGoogleからインストールされているが、これらのライブラリのどこにも "GsonConverterFactory"というJSONの解析に必要なクラスがありません。

編集:私はWindowsです。

答えて

16

レトロフィット2を使用している場合は、convert-gsonパッケージを含める必要があります。 gradleビルドの場合は、依存関係セクションにcompile 'com.squareup.retrofit:converter-gson:2.0.0-beta3'を追加できます。

その他のビルドシステムの場合、またはjarをダウンロードする場合は、Maven Central convert-gsonページをチェックアウトしてください。

18

build.gradleファイルにcompile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'を追加して、依存関係を解決するか、対応するjarをbulidパスに追加してください。その後、

私は2.0.0-beta1を使用してみましたが、下記のようそれは私の工場のための違法な型変換エラーを与えたので、だから私の提案はにある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 

に移動Converter Factory

を取得するためにGsonConverterFactory.create()を使用使用2.0.0-beta2

私のbuild.gradleは改造を解決するために以下の依存関係があります。

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' 
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' 
+1

iagreenによる回答は、ベータ2を改造するためには機能しません.. retrofit2が使用されている場合、私はconverter-gson beta 2を@manojのように使用する必要があります。 – DJphy

+0

は上記の依存関係 – NarendraJi

11

あなたのモジュールにこの

/* 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' 
8

をお試しください:アプリは、たとえば、あなたのretrofit2のバージョンが2.1であるので、上記のバージョンは、あなたのretrofit2のバージョンと同じである

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

を追加build.gradle .0、あなたのbuild.gradleは以下のようになります:

compile 'com.squareup.retrofit2:retrofit:2.1.0' 
compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
関連する問題