4

I have an Android Project and I need to use an http call.

I use this simply code:

public void onGetClick(View v) {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://www.google.com");
    HttpResponse response = client.execute(request);

    BufferedReader rd = new BufferedReader(new InputStreamReader
(response.getEntity().getContent()));

    String line = "";
    while ((line = rd.readLine()) != null) {
        Log.d("WM.ALS", line);
    }

}

but I cannot find the HttpClient library.

I added into dir /lib the .jar file downloaded from Apache (http://hc.apache.org/downloads.cgi) and into the "build.gradle(Module: app)" file, i added:

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.1.1'

  compile 'com.android.support:design:23.0.1'
  compile 'org.apache.httpcomponents:httpcore-4.4.3'
  compile 'org.apache.httpcomponents:httpclient-4.5.1'

}

But still don't work..

How do I do?

img.simone
  • 632
  • 6
  • 10
  • 23

3 Answers3

10

Add this in build.gradle (Module:app)

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

its working

RushDroid
  • 1,570
  • 3
  • 21
  • 46
4
  1. You are using appcompat-v7:23.1.1' .HttpClient deprecated in here .

HttpClient is not supported in SDK 23 .You need to use URLConnection.

An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length. This class may be used to send and receive streaming data whose length is not known in advance.

Please check HttpClient won't import in Android Studio

You can use org.apache.http.legacy

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file

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

Then Clean-Rebuild-Sync .Hope this helps.

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
2

As answered by @Rushabh042

    android{
            compileSdkVersion 23
            buildToolsVersion "23.0.2"
            useLibrary 'org.apache.http.legacy'
           }

will work like a charm...! Or you can do this by this way too:

Android studio->File->Project Structure->(Select your app in Modules section left side)Dependencies tab->Add dependency

Search for org.apache.http.legacy and you can select from the searched list.

Rebuild your app and there you go... :)

Happy Coding

Gaurav Karia
  • 166
  • 8