Java 如何在 RetroFit 中使用 Gson 转换器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32428472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:31:26  来源:igfitidea点击:

How do I use the Gson converter in RetroFit?

javaandroidgsonretrofit

提问by vic.vele

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

我正在制作一个用于教育目的的简单 RetroFit 应用程序,并使用 IntelliJ IDEA 作为我的 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.

我已经正确导入了 Retrofit 库(至少我认为我有),但我无法获得 Gson Converter 包。我安装了来自 google 的 gson.jar,但在这些库中的任何一个中都没有一个名为“GsonConverterFactory”的类,这是我解析 JSON 所必需的。

Edit: I'm on Windows.

编辑:我在 Windows 上。

回答by iagreen

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

如果您使用的是改造 2,则需要包含该convert-gson软件包。对于 gradle 构建,您可以添加compile 'com.squareup.retrofit:converter-gson:2.0.0-beta3'到您的依赖项部分。

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

对于其他构建系统,或要下载 jar,请查看Maven Central convert-gson页面。

回答by manoj

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

添加compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'到您的build.gradle文件并解决依赖项或将相应的 jars 添加到您的 bulid 路径。

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

然后用GsonConverterFactory.create()得到Converter Factory

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

我尝试使用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

So my suggestion is to use 2.0.0-beta2

所以我的建议是使用 2.0.0-beta2

my build.gradle has following dependencies to resolve retrofit.

我的 build.gradle 有以下依赖项来解决改造问题。

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

回答by Joolah

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'

回答by faruk

in your Module:app build.gradle add

在你的 Module:app build.gradle 添加

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 :

上面的版本与您的retrofit2版本相同,因此例如您的retrofit2版本是2.1.0,那么您的build.gradle应该是这样的:

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