无法使用GSON库解析java.lang.NoSuchMethodError

moo*_*oon 6 java jar gson retrofit retrofit2

我试图建立一个样本Retrofit Java程序取自;

https://github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/SimpleService.java

我把所需的依赖罐(retrofit-2.0.0.jar,converter-gson-2.0.0.jar,okhttp-3.0.0-RC1.jar,okio-1.6.0.jar和gson-2.0.jar)包括在内建立路径.

尝试运行应用程序时遇到以下异常.

Exception in thread "main" java.lang.NoSuchMethodError: com.google.gson.Gson.getAdapter(Lcom/google/gson/reflect/TypeToken;)Lcom/google/gson/TypeAdapter;
at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:63)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:325)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:308)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:651)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:166)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at com.sun.proxy.$Proxy0.contributors(Unknown Source)
at SimpleService.main(SimpleService.java:40)
Run Code Online (Sandbox Code Playgroud)

我错过了任何依赖的罐子吗?

我试图在独立的Java应用程序中使用Retrofit,并且正在测试样本以便习惯它.如果有人能指出一个简单的示例程序来演示Retrofit的工作,那将会很棒,即使早期版本的样本也会很好.

小智 8

尝试在gradle中添加:

compile 'com.google.code.gson:gson:2.6.2'
Run Code Online (Sandbox Code Playgroud)

这对我来说很有效 :)


raf*_*kob 0

尝试这个:

final RETROFIT_VERSION = '2.0.0'
final OKHTTP_VERSION = '3.2.0'

compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
compile "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION"
compile "com.squareup.okhttp3:okhttp-urlconnection:$OKHTTP_VERSION"

compile 'com.google.code.gson:gson:2.4'
Run Code Online (Sandbox Code Playgroud)