59

Android has deprecated the Apache module since API level 22, so my question is, how do I use, for example HttpResponse from the Apache library, not from Android SDK? The problem is that the're the same in both packages.

But, for example, HttpGet is OK, because it's called HttpGetHC4 in Apache.

Jakub Šturc
  • 32,938
  • 24
  • 85
  • 107
Evgeniy
  • 591
  • 1
  • 4
  • 4
  • Just read docs and review sample apps at the apache link. Where namespaces collided u see the suffix=HC4 – Robert Rowntree Mar 30 '15 at 20:36
  • check out this info for complete details, best explanation http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153 – ked Sep 24 '16 at 14:53

8 Answers8

41

The method HttpClient was deprecated. You can now use the URLConnection as you can see in this example:

private StringBuffer request(String urlString) {
    // TODO Auto-generated method stub

    StringBuffer chaine = new StringBuffer("");
    try{
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestProperty("User-Agent", "");
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.connect();

        InputStream inputStream = connection.getInputStream();

        BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        while ((line = rd.readLine()) != null) {
            chaine.append(line);
        }
    }
    catch (IOException e) {
        // Writing exception to log
        e.printStackTrace();
    }
    return chaine;
}

I hope this is helping someone.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
vincent091
  • 2,245
  • 4
  • 15
  • 21
  • I have done the same thing but the problem it doesnt read the opening bracket of the JSON OBJECT which is the only character in a line. For eg : {name would be read properly but { new line name (in this case the opening bracket wont be read properly ) – Sagar Devanga Sep 10 '15 at 11:32
31

There is nothing bad with using Apache's modules. Google just made a big mess of it, because they failed to make a successful fork. Google and Apache integration was supervised by Jesse Wilson - he worked in Google, messed up everything and then made his own library (OkHttp) during work in square. That's a really ugly story.

I advice against using legacy the JAR file because it contains an old version of Apache libraries without improvements and bugfixes (it is a very old pre-BETA snapshot). As you can see on the official page of Apache components, there is a fresh 4.4 version, compatible with all of Android versions. It just had to be repackaged under different namespace to avoid collision with old version.

You simply can add the dependency from Maven (or download release from GitHub):

dependencies {
    compile "cz.msebera.android:httpclient:4.4.1.2"
}

And then replace org.apache.http with cz.msebera.android.httpclient, so your imports will look like:

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpHost;
import cz.msebera.android.httpclient.HttpResponse;

Yeah, you can continue using Apache libraries without wasting hours of rewriting of working code!

As for the difference between using Apache components and HttpURLConnection, HttpURLConnection uses responce caching... And that's all. I'm not sure that you really want it and also you can always implement it yourself.

By the way, I tried alternatives like HttpURLConnection - those are not even close to Apache's power and simplicity.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Jehy
  • 4,273
  • 1
  • 33
  • 54
  • 2
    I changed the implementation after spending several hours on HttpUrlConnection. You're right. That's not even close to the apache library. Can anyone explain, why the client of msebera is recommended for apps targeting SDK 23, and the "official one" is only for devices targeting <= SDK 22 (see here: https://hc.apache.org/httpcomponents-client-4.5.x/android-port.html) I don't fully understand that.. Thanks – AndyB Jan 16 '17 at 09:01
  • 2
    This should be the answer! Thanks man! I could change everything without my app breaking. No more deprecated crossings!! :) – Christopher Smit Jul 14 '17 at 10:44
  • 2
    Great answer, explaining the reason why we should NOT be afraid of using apache libs. With all warnings across the web we think it's a bad idea do use them, but after read this and check some points, you save the day. thanks! – rmpt Sep 05 '18 at 11:33
  • 1
    You saved my day. This is the better solution to provide back compatibility. Only after most users upgraded to Android 5.0+ I would consider using okhttp. – Joe Tse Feb 21 '19 at 04:33
22

In Android Marshmallow (sdk 23), you can add:

useLibrary 'org.apache.http.legacy'

to build.gradle in the android {} section as a workaround. This seems to be necessary for some of Google's own gms libraries!

fattire
  • 5,004
  • 2
  • 18
  • 32
  • 1
    I try to add this but I have this error: Error:(55, 0) Gradle DSL method not found: 'useLibrary()' – Jachumbelechao Unto Mantekilla Sep 09 '15 at 11:37
  • 3
    I have to change my gradle version in the buildsrcipt classpath 'com.android.tools.build:gradle:1.3.1' . Now works – Jachumbelechao Unto Mantekilla Sep 09 '15 at 13:37
  • 1
    DO NOT use this way. It's only working before Android 8.0. Android 8.0+ forces deprecation of org.apache.http which causes crashes. I've been hurt so much. – Joe Tse Feb 21 '19 at 04:35
  • 1
    @JoeTse: We had that problem with Android 9, but there's a work-around in the [release notes](https://developer.android.com/about/versions/pie/android-9.0-changes-28) - see the manifest change to allow HttpClient. The other option is to use another HttpClient build from Apache or someone else. – Nick Westgate Aug 15 '19 at 06:20
11

If I were you I do not use HttpClient because:

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

For Gingerbread and better, HttpURLConnection is the best choice.

Reference

Use OKHttp or HttpUrlConnection. And also I recommend not using the Apache library, because it may not be efficient for Android.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
mmlooloo
  • 18,037
  • 5
  • 41
  • 60
6

I guess you could use the Apache libraries directly in your project, and not the ones shipped with Android (you can get more control this way - and compile with an older SDK version if you need to).

In your build.gradle, add this line :

dependencies {
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

To prevent warnings, add these lines as well :

android {

     -- YOUR EXISTING LINES --

     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'
    }

}
Vivi
  • 932
  • 14
  • 30
4

Add this to the dependencies of your app, and then it will work correctly:

dependencies {
...
  compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
1

If still want to use httpclient library, you should add some description to the build.gradle file. And it seems that the content added to build.gradle file is determined by the target Android project build for.

if targeted for API22 and older, then should add the following line into build.gradle

dependencies {
    compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'
}

if targeted for API23 and later, then should add the following line into build.gradle

dependencies {
    compile group: 'cz.msebera.android' , name: 'httpclient', version: '4.4.1.1'
}

Here is the reference link

A-Shi
  • 31
  • 3
-2

Use openConnection().

The HttpClient documentation suggests that.

Refer this answer.

Community
  • 1
  • 1
priyank
  • 2,553
  • 4
  • 21
  • 33