1

I have tons of deprecation because of http client related classes.

Here my code:

    this.httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = this.httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    int statusCode = response.getStatusLine().getStatusCode();
     if (statusCode == HttpStatus.SC_OK) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(entity.getContent(),
                        EntityUtils.getContentCharSet(entity))
        );
        String line = null;
        Matcher matcher = null;
        while ((line = reader.readLine()) != null) {
            matcher = matcher == null ? this.resultPattern
                    .matcher(line) : matcher.reset(line);
            if (matcher.matches()) {
                httpget.abort();
                return Double.parseDouble(matcher.group(1));
            }
        }
        httpget.abort();
        throw new MyException("Could not find ["
                + resultPattern.toString() + "] in response to [" + url
                + "]");
    } else {
        if (entity != null) {
            entity.consumeContent();
        }
        throw new MyException("Got [" + statusCode
                + "] in response to [" + url + "]");
    }

DefaultHttpClient deprecated HttpResponse deprecated HttpEntity deprecated

How could I fix using native libraries? I have searched and some people for HttpClient use HttpClientBuilder but requires an extra library and in addition I have no idea how to fix the other deprecation problems.

Is there some programmer that could help me? What is the reason of this massive deprecation?

Seems that now we should use HttpURLConnection, but I haven't understood how to migrate my code to these libraries.

AndreaF
  • 11,975
  • 27
  • 102
  • 168

4 Answers4

0

How could I fix using native libraries?

I have no idea what you consider "native libraries" to be in this context. Android's built-in edition of HttpClient was deprecated with Android 5.1, and it is removed entirely in the M Developer Preview.

If you need to stick with the Apache HttpClient API, switch to Apache's edition of their library.

Or, switch to any number of other HTTP APIs, such as the built-in HttpUrlConnection, OkHttp, etc.

What is the reason of this massive deprecation?

Google has indicated for a few years that you should not use HttpClient; they are now merely enforcing this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the answer, could you give me a sample based on my code? Seems pretty different the syntax that I must use to perform the same operations – AndreaF Jul 21 '15 at 12:25
  • OkHttp is close enough to HttpClient, you can mostly do the same (basic) things with very little changes in your code, IIRC – njzk2 Jul 21 '15 at 17:54
0

I have build a HttpUtil class that uses standard Java Library. You can modify it feither. I just set everything as map then you can easly send a Post or Get.

HttpURLConnection urlConnection;
String CHAR_ENCODING = "UTF-8";
String CONTENT_TYPE_X_WWW_FORM = "application/x-www-form-urlencoded;charset=UTF-8";

public String sendHttpPostRequestWithParams(String url, Map<String, Object> httpPostParams) {
    try {
        byte[] postDataBytes = setParameters(httpPostParams).toString().getBytes(CHAR_ENCODING);

        URL post = new URL(url);
        urlConnection = (HttpURLConnection) post.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("Accept-Charset", CHAR_ENCODING);
        urlConnection.setRequestProperty("Content-Type", CONTENT_TYPE_X_WWW_FORM);
        urlConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        urlConnection.getOutputStream().write(postDataBytes);

        return convertStreamToString(urlConnection.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
        return Constants.GSON.toJson(new BaseResponse(ResponseCodes.SERVICE_NOT_FOUND.getCode(),
                ResponseCodes.SERVICE_NOT_FOUND.getMessage()));
    }
}

public String sendHttpGetRequestWithParams(String stringUrl, Map<String, Object> params) {
    try {
        URL url = new URL(stringUrl + "?" + setParameters(params));
        urlConnection = (HttpURLConnection) url.openConnection();

        return convertStreamToString(urlConnection.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
        return Constants.GSON.toJson(new BaseResponse(ResponseCodes.SERVICE_NOT_FOUND.getCode(),
                ResponseCodes.SERVICE_NOT_FOUND.getMessage()));
    }
}

public String setParameters(Map<String, Object> params) {
    try {
        StringBuilder parameters = new StringBuilder();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            if (parameters.length() != 0) {
                parameters.append('&');
            }
            parameters.append(URLEncoder.encode(param.getKey(), CHAR_ENCODING));
            parameters.append('=');
            parameters.append(URLEncoder.encode(String.valueOf(param.getValue()), CHAR_ENCODING));
        }
        return parameters.toString();
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

public String convertStreamToString(InputStream inputStream) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String temp, response = "";
        while ((temp = reader.readLine()) != null) {
            response += temp;
        }
        return response;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Simon Mokhele
  • 3,913
  • 3
  • 21
  • 22
0

As Google documentation says you can still use HTTP Apache API interfaces. Declare following dependency in your gradle.build file:

android {
    useLibrary 'org.apache.http.legacy'
}
Alex Kucherenko
  • 20,168
  • 2
  • 26
  • 33
-1

If you are developing for Android you should move to Volley already, this is much better. These methods are deprecated and there are no others native libraries as i remember.

Peyphour
  • 43
  • 1
  • 5