0

MainActivity.java

public class MainActivity extends AppCompatActivity {

   Button clickButton;
   protected void onCreate(Bundle savedInstanceState)
   {

       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       clickButton = (Button) findViewById(R.id.clickButton);
       clickButton.setOnClickListener(new View.OnClickListener()
       {
           public void onClick(View v)
           {

              String serverURL= ""
              new MyClass().execute("serverURL");
           }
       });

    private class MyClass extends AsyncTask<String, Integer, String>
    {

        TextView output = (TextView) findViewById(R.id.textView1);
        private ProgressDialog Dialog = new    ProgressDialog(MainActivity.this);

        protected void onPreExecute() {

            output.setText("Output : ");
            Dialog.setMessage("Downloading source..");
            Dialog.show();
        }

        protected String doInBackground(String... urls)
        {
            HttpResponse<JsonNode> response=null;
            try {
            }


            response = Unirest.post("https://....?api_key=""&api_secret=")
                    .header("X-Mashape-Key", "")
                    .header("Content-Type", "application/x-www-form-
                            urlencoded")
                                    .header("Accept", "application/json")
                                    .field("attributes", "all")
                                    .field("detector", "Aggressive")
                                    .field("urls", urls[0])
                                    .asJson();


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return response.toString();


    }

protected void onProgressUpdate(Integer...integers) {
}

protected void onPostExecute(String response) {
    output = (TextView) findViewById(R.id.textView1);
    output.setText(response);
}

}
}

ERRORS

An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.jav at java.util.concurrent.FutureTask.setExcept at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) : An error occured while executing doInBackground() android.os.AsyncTask$3.done(AsyncTask.java:299) java.util.concurrent.FutureTask$Sync.innerSetException at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:137) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) Caused by: java.lang.ExceptionInInitializerError at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151) at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131) at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68) at com.example.preeti.button.MainActivity$MyClass.doInBackground(MainActivity.java:91) at com.example.preeti.button.MainActivity$MyClass.doInBackground(MainActivity.java:60) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) at java.util.concurrent.FutureTask.run(FutureTask.java:137)  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)  at java.lang.Thread.run(Thread.java:856)  Caused by: java.lang.NoClassDefFoundError: org.apache.http.client.config.RequestConfig at com.mashape.unirest.http.options.Options.refresh(Options.java:70) at com.mashape.unirest.http.options.Options.(Options.java:46) at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151)  at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)  at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)  at com.example.preeti.button.MainActivity$MyClass.doInBackground(MainActivity.java:91)  at com.example.preeti.button.MainActivity$MyClass.doInBackground(MainActivity.java:60)  at android.os.AsyncTask$2.call(AsyncTask.java:287)  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)  at java.util.concurrent.FutureTask.run(FutureTask.java:137)  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)  at java.lang.Thread.run(Thread.java:856) 

Build.gradle

apply plugin: 'com.android.application'


android {
compileSdkVersion 23
buildToolsVersion "24.0.0"

defaultConfig {
    applicationId "com.example.preeti.button"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
    }}
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.github.mhendred.face4j:face4j-core:1.6.2'
compile 'com.mashape.unirest:unirest-java:1.4.9'
compile 'com.google.android.gms:play-services-appindexing:8.1.0'
compile 'org.json:json:20140107'
compile 'org.apache.httpcomponents:httpcomponents-client:4.5.2'
compile 'com.loopj.android:android-async-http:1.4.9'



}
Sentry
  • 4,102
  • 2
  • 30
  • 38
  • Add the code that does the request in the `try` block. An empty `try` block is of no use. – Prerak Sola Jul 29 '16 at 15:36
  • Can someone please check if I put the AsyncTask task correctly in this activity? I think I've made a mistake somewhere. – Preeti Bansal Jul 29 '16 at 15:37
  • read the changelog: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client – njzk2 Jul 29 '16 at 16:01
  • Thankyou for your help @Preak Sola..but it still shows the same errors – Preeti Bansal Jul 29 '16 at 18:04
  • 1
    1) You don't need an AsyncTask to use Unirest methods 2) just because Unirest supports Java, doesn't mean it necessarily supports Android. Okhttp is the closest alternative to the code you've written – OneCricketeer Jul 31 '16 at 13:27
  • Even easier - use Retrofit, which gives you a nice, high-level REST abstraction on top of Okhttp – GreyBeardedGeek Jul 31 '16 at 14:28

2 Answers2

0

It seems Mashape is still working with the deprecated Apache HTTP-Library. You can reduce your target-SDK to 22 or include

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

in your apps build.gradle file if this is the reason.

Christian
  • 4,596
  • 1
  • 26
  • 33
  • Thankyou for your help @Christian...i included but it still shows the same error – Preeti Bansal Jul 29 '16 at 15:56
  • try to clean the project and re-run. The errorlog shows NoClassDefError for the class: org.apache.http.client.config.RequestConfig - if you still have the problem after clean, you can try to initialize one object of the HTTP-Library and see if it works. – Christian Jul 29 '16 at 15:59
  • Thankyou for your time..but it is not working...still shows same warnings – Preeti Bansal Jul 29 '16 at 18:59
0
  Caused by: java.lang.NoClassDefFoundError:
  org.apache.http.client.config.RequestConfig
    at
  com.mashape.unirest.http.options.Options.refresh(Options.java:70)

unirest is trying to access a class from apache http client that doesn't exist. Find a version of unirest that mactches apache http client or the other way around.

Also put your failing code inside the try { ... }.

UPDATE

Following are the dependencies of unirest 1.4.9 from the pom file. Try to add them to your project dependencies.

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160212</version>
    </dependency>
</dependencies>

That would make your dependencies look like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.github.mhendred.face4j:face4j-core:1.6.2'
    compile 'com.mashape.unirest:unirest-java:1.4.9'
    compile 'com.google.android.gms:play-services-appindexing:8.1.0'
    compile 'com.loopj.android:android-async-http:1.4.9'
    compile 'org.json:json:20160212'
    compile 'org.apache.httpcomponents:httpclient:4.5.2'
    compile 'org.apache.httpcomponents:httpasyncclient:4.1.1'
    compile 'org.apache.httpcomponents:httpmime:4.5.2'
}

Notice the last 4 lines.

UPDATE 2

There are 2 issues. org.json and org.apache.http.client are already a part of the Android API.

This link shows how to use unirest-java on android.

You are dealing with a lot of garbage I think. Maybe consider using something other than unirest-java on Android.

totoro
  • 2,469
  • 2
  • 19
  • 23
  • Thankyou For your help @totoro ... i done with try block but i am not able to figure out what version of unirest i will use.. – Preeti Bansal Jul 29 '16 at 19:20
  • @cricket_007 was in the process. – totoro Jul 31 '16 at 13:21
  • It should be mentioned that there is a different dependency for the Android Apache HTTP port. Those dependencies are strictly for Java. For example, `org.json` shouldn't be necessary as it conflicts with the Android JSON classes – OneCricketeer Jul 31 '16 at 13:24