Java 如何在改装中使用Gson转换器?
我正在制作一个用于教育目的的简单改造应用程序,并使用IntelliJ IDEA作为我的IDE 我已经正确地导入了改装库(至少我认为我已经导入了),但是我无法获得Gson转换器包。我已经安装了google的gson.jar,但在这两个库中都没有一个名为“GsonConverterFactory”的类,我需要它来解析JSONJava 如何在改装中使用Gson转换器?,java,android,gson,retrofit,Java,Android,Gson,Retrofit,我正在制作一个用于教育目的的简单改造应用程序,并使用IntelliJ IDEA作为我的IDE 我已经正确地导入了改装库(至少我认为我已经导入了),但是我无法获得Gson转换器包。我已经安装了google的gson.jar,但在这两个库中都没有一个名为“GsonConverterFactory”的类,我需要它来解析JSON 编辑:我在Windows上。如果您使用的是改装2,则需要包括convert gson软件包。对于gradle版本,您可以将编译'com.squareup.reformation
编辑:我在Windows上。如果您使用的是改装2,则需要包括
convert gson
软件包。对于gradle版本,您可以将编译'com.squareup.reformation:converter gson:2.0.0-beta3'
添加到依赖项部分
对于其他构建系统,或者要下载jar,请查看Maven中心页面。将
编译'com.squareup.regent:converter gson:2.0.0-beta2'
添加到build.gradle
文件中,并解决依赖关系,或者将相应的jar添加到bulid路径中
然后使用GsonConverterFactory.create()
获取转换器工厂
我尝试使用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
所以我的建议是使用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
my build.gradle具有以下依赖项以解决改装问题
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
试试这个
/* 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'
在您的模块中:app build.gradle add
compile 'com.squareup.retrofit2:converter-gson:[retrofit2 version]'
上面的版本与您的改型2版本相同,因此,例如,您的改型2版本是2.1.0,而build.gradle应该是这样的:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
iagreen的回答不适用于beta2的改造。。如果使用改型2,我们需要使用@manoj.所说的转换器gson beta 2。对于上述依赖项,我们得到了错误-无法解决-这对我有效-指定它们必须完全相同