无法使用 GSON 库解决 java.lang.NoSuchMethodError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36211510/
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
Not able to resolve java.lang.NoSuchMethodError with GSON library
提问by moooni moon
I was trying to build a sample Retrofit Java program taken from;
我试图构建一个示例 Retrofit Java 程序;
I included required dependent jars (retrofit-2.0.0.jar,converter-gson-2.0.0.jar,okhttp-3.0.0-RC1.jar,okio-1.6.0.jar and gson-2.0.jar) to the build path.
我将所需的依赖 jar(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)包含在构建路径。
I encounter the following exception while trying to run the application.
我在尝试运行应用程序时遇到以下异常。
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.invoke(Retrofit.java:145)
at com.sun.proxy.$Proxy0.contributors(Unknown Source)
at SimpleService.main(SimpleService.java:40)
Am I missing any dependent jar?
我是否缺少任何依赖 jar?
I am trying to use Retrofit in standalone Java application and was testing samples to get used to it. It will be great if someone could point me to a simple sample program that demonstrates the working of Retrofit, even a sample of earlier version would be good.
我正在尝试在独立的 Java 应用程序中使用 Retrofit,并且正在测试示例以适应它。如果有人能指点我一个简单的示例程序来演示 Retrofit 的工作,那就太好了,即使是早期版本的示例也很好。
回答by Yakami
try to add this in gradle:
尝试在gradle中添加这个:
compile 'com.google.code.gson:gson:2.6.2'
it works well for me :)
这对我来说很有效 :)
回答by DearDhruv
Here are some dependencies that might help you.
以下是一些可能对您有所帮助的依赖项。
GSON and Retrofit
GSON 和改造
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
HTTP
HTTP
compile 'org.apache.commons:commons-lang3:3.4'
compile 'org.apache.httpcomponents:httpcore:4.4.5'
Try to clean your project and sync with gradle. Or try using the same version jar files in your project.
尝试清理您的项目并与 gradle 同步。 或者尝试在您的项目中使用相同版本的 jar 文件。
Or have you replaced this line with correct value.
或者您是否用正确的值替换了此行。
@GET("/repos/{owner}/{repo}/contributors")
What is the response after calling the API. plz check the URL in the browser.
调用API后的响应是什么。请检查浏览器中的 URL。
let me know if issue still persists.
如果问题仍然存在,请告诉我。
回答by Hafaiedh Chedly
I had the same problem when I used the Alfresco Rest API in my JEE Application I solved the problem by adding these Maven dependencies
我在 JEE 应用程序中使用 Alfresco Rest API 时遇到了同样的问题,我通过添加这些 Maven 依赖项解决了这个问题
<properties>
<retrofit.version>2.1.0</retrofit.version>
<okhttp.version>3.4.1</okhttp.version>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>adapter-rxjava</artifactId>
<version>${retrofit.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp.version}</version>
</dependency>
</dependencies>
Or you can add the correct Libraries (Jar files) in your build path if your are not using maven
或者,如果您不使用 maven,您可以在构建路径中添加正确的库(Jar 文件)
I hope this will help you
我希望这能帮到您
回答by seekingStillness
Late answer, yes, but I struggled with this for a week. Wanted to post the solution that worked for me.
迟到的答案,是的,但我为此苦苦挣扎了一个星期。想发布对我有用的解决方案。
If you get this issue it could be from the existing crawler being FUBAR. As a workaround you can go back to the previous version of the crawler by opting out of the newly released one.
如果您遇到此问题,可能是因为现有的爬虫是 FUBAR。作为一种解决方法,您可以通过选择退出新发布的爬虫来返回到先前版本的爬虫。
Here's how to opt-out:
以下是选择退出的方法:
Sign in to your Play Console. Select an app. Select Release management > Pre-launch report > Settings. In the “Pre-launch report version” section, move the Opt-out switch to the right until it turns blue. After this, the launch reports appear correctly again.
登录您的 Play 管理中心。选择一个应用程序。选择发布管理 > 预发布报告 > 设置。在“预发布报告版本”部分,将选择退出开关向右移动,直到它变成蓝色。在此之后,启动报告再次正确显示。
回答by rafakob
Try this:
试试这个:
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'