【问题标题】:Getting Failed resolution of: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor When trying to implement a Unirest API from RapidAPI获取失败的解决方案:Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor 尝试从 RapidAPI 实现 Unirest API 时
【发布时间】:2018-09-19 11:08:39
【问题描述】:

我正在尝试使用 Android 实现 RapidApi 的 Spoonacular Api。它使用 Unirest,所以我正在尝试配置它。在official documentation 我关注Without Maven 部分。我下载了 unirest jar,并在我的 build.gradle 中有这些

    apply plugin: 'com.android.application'

    android {
             .......
        useLibrary  'org.apache.http.legacy'

        packagingOptions {
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/ASL2.0'
        }



    }

    dependencies {
        ......

        implementation 'com.rapidapi:rapidconnect-android:0.1'
        implementation files('libs/unirest-java-1.4.9.jar')

        implementation 'org.json:json:20171018'

        implementation('org.apache.httpcomponents:httpmime:4.3.6') {
            exclude module: 'httpclient'
        }
        implementation 'org.apache.httpcomponents:httpclient-android:4.3.5'


    }

    apply plugin: 'com.google.gms.google-services'

这是我的java代码:

HttpResponse<JsonNode> response = null;
        try {
            response = Unirest.get("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=egg%2Cyogurt&number=5&ranking=1")
                    .header("X-Mashape-Key", "keyyy")
                    .header("X-Mashape-Host", "spoonacular-recipe-food-nutrition-v1.p.mashape.com")
                    .asJson();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

我收到此错误:

09-19 11:53:19.674 6789-6789/com.example.hassen.androidpim E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.hassen.androidpim, PID: 6789
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/nio/reactor/DefaultConnectingIOReactor;
        at com.mashape.unirest.http.options.Options.refresh(Options.java:85)
        at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
        at com.mashape.unirest.http.options.Options.getOption(Options.java:42)

您能帮我弄清楚如何解决这个问题吗? 谢谢

【问题讨论】:

  • NoClassDefFoundError: 好像少了点什么。比如,httpasyncclient 可能吗?来自文档:Don't forget to also install the dependencies (org.json, httpclient 4.3.6, httpmime 4.3.6, httpasyncclient 4.0.2) in the classpath too.
  • 这不是像我一样将它们包含在 build.gradle 文件中的依赖项下吗?

标签: android json android-gradle-plugin httpresponse unirest


【解决方案1】:

我相信您错过了其中一个 Apache 依赖项(我想在您的情况下是httpasyncclient)。

正如the documentation 所提到的,实际上有其中三个

  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>

  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>

  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>

因此,请转到此链接下载 - 在您的项目中手动添加 jar 文件:

http://hc.apache.org/downloads.cgi

然后寻找:HttpAsyncClient


使用它在您的项目中添加 jar 文件库:How to add a jar in External Libraries in android studio

【讨论】:

  • 非常感谢,现在我已经添加了所有依赖项,如下所示imgur.com/a/Cd2OpCX 但我收到了类似的错误 java.lang.NoSuchFieldError: No field INSTANCE of type Lorg/apache/http/conn /ssl/AllowAllHostnameVerifier;在 Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier 类中;或其超类('org.apache.http.conn.ssl.AllowAllHostnameVerifier' 的声明出现在 /system/framework/framework.jar:classes2.dex 中)我查了一下,它似乎与httpclient 有关。 httpclient 已添加,我不知道到底是什么问题
  • 同样的问题:stackoverflow.com/questions/27366430/… 另外,json 依赖似乎也存在问题。
猜你喜欢
  • 2014-12-30
  • 2018-10-31
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
相关资源
最近更新 更多