Android deprecated apache module (HttpClient, HttpResponse, etc.)

霸气de小男生 提交于 2019-11-26 01:34:36

问题


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.


回答1:


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.




回答2:


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!




回答3:


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.




回答4:


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

}



回答5:


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



回答6:


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




回答7:


Use openConnection().

The HttpClient documentation suggests that.

Refer this answer.



来源:https://stackoverflow.com/questions/29294479/android-deprecated-apache-module-httpclient-httpresponse-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!