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

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.

Leave a Comment