2

I am trying to implement the Spoonacular Api from RapidApi with Android. It uses Unirest so I am trying to configure it. On the official documentation I am following the Without Maven section. I downloaded the unirest jar and I have these in my 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'

and this is my java code:

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();
        }

I am getting this error:

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)

Could you please help me figure out how to fix this? Thank you

h_h10a
  • 449
  • 5
  • 18
  • 1
    **NoClassDefFoundError:** Seems like something is missing. Like, `httpasyncclient` maybe? From the documentation : `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.` – ʍѳђઽ૯ท Sep 19 '18 at 11:31
  • 1
    Doesn't that mean including them under dependencies in the `build.gradle` file, like i did? – h_h10a Sep 19 '18 at 11:33

1 Answers1

1

I believe you missed one of those Apache dependencies (httpasyncclient in your case I suppose).

There are actually three of them as mentioned by 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>

So, go to this link download-add the jar file manually inside your project:

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

And look for: HttpAsyncClient.


Use this to add jar file-library inside your project: How to add a jar in External Libraries in android studio

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • Thank you so much, now i have all the dependencies added as shown here https://imgur.com/a/Cd2OpCX but im getting a similar error java.lang.NoSuchFieldError: No field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar:classes2.dex) I looked it up and it seems like it's related to `httpclient`. `httpclient` is added and i dont know what exactly the problem is – h_h10a Sep 19 '18 at 12:57
  • Same question: https://stackoverflow.com/questions/27366430/getting-nosuchfielderror-instance-org-apache-http-message-basicheadervalueparser Also, it seems like there is an issue with the json dependency too. – ʍѳђઽ૯ท Sep 19 '18 at 14:45