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

无法使用GSON库解决java.lang.NoSuchMethodError,java,jar,gson,retrofit,retrofit2,Java,Jar,Gson,Retrofit,Retrofit2,我正试图构建一个示例改装Java程序,该程序取自: 我在构建路径中包括了所需的依赖jar(reformation-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(L

我正试图构建一个示例改装Java程序,该程序取自:

我在构建路径中包括了所需的依赖jar(reformation-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)
我是否缺少任何依赖的罐子

我试图在独立Java应用程序中使用改型,并测试了一些示例以适应它。如果有人能给我一个简单的示例程序来演示改装的工作原理,那就太好了,即使是早期版本的示例也不错。

试试这个:

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'

尝试在gradle中添加以下内容:

compile 'com.google.code.gson:gson:2.6.2'

它对我来说很好:)

以下是一些可能对您有所帮助的依赖项

GSON及其改造 超文本传输协议 尝试清理项目并与gradle同步。 或尝试在项目中使用相同版本的jar文件。

或者用正确的值替换了这一行

@GET("/repos/{owner}/{repo}/contributors")
调用API后的响应是什么。 请检查浏览器中的URL


如果问题仍然存在,请告诉我。

我在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>

2.1.0
3.4.1
com.squareup.2
改造
${reformation.version}
com.squareup.2
转换器gson
${reformation.version}
com.squareup.2
适配器rxjava
${reformation.version}
com.squareup.okhttp3
okhttp
${okhttp.version}
com.squareup.okhttp3
日志拦截器
${okhttp.version}
如果不使用maven,也可以在构建路径中添加正确的库(Jar文件)


我希望这能帮助你

迟来的回答,是的,但我为此挣扎了一周。想发布对我有效的解决方案

如果你得到这个问题,它可能是从现有的爬虫被FUBAR。作为一种解决方法,您可以选择退出新发布的爬虫程序,从而返回到以前版本的爬虫程序

以下是如何选择退出:


登录到您的游戏机。选择一个应用程序。选择发布管理>预发布报告>设置。在“预启动报告版本”部分,向右移动选择退出开关,直到其变为蓝色。在此之后,启动报告再次正确显示。

我使用了我提到的链接中的代码-我认为当JVM在类路径中找不到时会抛出此错误?因此,您认为我没有添加其他必需的库吗?我已经按照您的建议使用了jar版本,但仍然出现以下错误。线程“main”java.lang.NoSuchMethodError中出现异常:com.google.gson.gson.newJsonReader(Ljava/io/Reader;)Lcom/google/gson/stream/JsonReader;在2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:35)中,我当前的类路径有以下JAR。okio-1.6.0.jar改型-2.0.0.jar okhttp-3.2.0.jar okhttp-urlconnection-3.2.0.jar gson-2.4.jar converter-gson-2.0.0.jarRemove
jar
文件和他对grableI说的话我不使用gradle甚至Maven,我的项目使用ANT,这是一个windows独立应用程序。我不确定您是否可以使用ANT来执行此操作。
gson 2.8.0
给了我
NoSuchMethodError
,降级到
2.6.2
有效。
@GET("/repos/{owner}/{repo}/contributors")
<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>