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

Evgeniy picture Evgeniy · Mar 27, 2015 · Viewed 74.7k times · Source

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.

Answer

vincent091 picture vincent091 · Apr 27, 2015

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.