Mira Stinktshir
Mira Stinktshir

Reputation: 303

Not able to use Apache HttpClient

I want to include Apache HttpClient 4.1 to my project.

So, I added to my build.gradle:

dependencies {
    ..
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.1'
}

Running the the sanity test:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        try {
            EntityUtils.consume(null);
        } catch (Exception e){
            System.out.print(e.getMessage());
        }
    }

gives the Error:

java.lang.NoSuchMethodError: No static method consume(Lorg/apache/http/HttpEntity;)V in class Lorg/apache/http/util/EntityUtils; or its super classes (declaration of 'org.apache.http.util.EntityUtils' appears in /system/framework/org.apache.http.legacy.boot.jar)

So why is Android calling org.apache.http.legacy.boot.jar, instead of calling the added Apache HttpClient 4.1 library? And how could I force to call it?

Upvotes: 4

Views: 1259

Answers (2)

Kiwi Lin
Kiwi Lin

Reputation: 811

Try add to your build.gradle

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

Upvotes: 1

Laith Alnagem
Laith Alnagem

Reputation: 654

Android 6.0 release removes support for the Apache HTTP client.

Read more details here: https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-apache-http-client

If you are set on using 4.1 instead of latest version try adding this dependency:

 compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.1.3'

After you build, you may want to restart your Android Studio just to make sure its not some strange glitch.

Upvotes: 1

Related Questions