Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.apache.http packages removed in API level 23. What is the alternative?

After adding following changes in by build.gradle right after updating to latest android API level 23 (Marshmallow) all org.apache.http classes is not working.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.myapp.package"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "1.1"
    }
}

I checked "Android API Differences Report" here. Its says all Classes of org.apache.http has been removed. Can someone suggest what is the alternative ?

Here is my code :

try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(address);

            httpPost.setEntity(new StringEntity("{\"longUrl\":\""+longUrl+"\"}"));
            httpPost.setHeader("Content-Type", "application/json");
            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
like image 748
techierishi Avatar asked Aug 25 '15 11:08

techierishi


People also ask

What is http client Android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.


1 Answers

See the Behavior Changes at Android Developers where it says that:

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' 
} 
like image 134
MidasLefko Avatar answered Sep 23 '22 23:09

MidasLefko