首页 文章

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

提问于
浏览
5

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

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

我把所需的依赖 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)包括在内 Build 路径 .

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

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)

我错过了任何依赖的 jar 吗?

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

5 回答

  • 0

    尝试在gradle中添加:

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

    这对我来说很有效 :)

  • 0

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

    GSON和Retrofit

    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

    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'org.apache.httpcomponents:httpcore:4.4.5'
    

    尝试清理项目并与gradle同步 . Or try using the same version jar files in your project.

    或者您是否用正确的值替换了这一行 .

    @GET("/repos/{owner}/{repo}/contributors")
    

    调用API后的响应是什么 . plz检查浏览器中的URL .

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

  • 5

    试试这个:

    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'
    
  • 2

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

    或者,如果您不使用maven,则可以在构建路径中添加正确的库(Jar文件)

    我希望这能帮到您

  • 0

    迟到的答案,是的,但我在这一周努力了 . 想发布适合我的解决方案 .

    如果您遇到此问题,可能是现有的爬虫是FUBAR . 作为一种解决方法,您可以通过选择退出新发布的爬虫来回到之前版本的爬虫 .

    以下是选择退出的方法:

    登录您的Play控制台 . 选择一个应用程序选择版本管理>启动前报告>设置 . 在“发布前报告版本”部分中,将选择退出开关向右移动直至其变为蓝色 . 在此之后,再次正确显示启动报告 .

相关问题