-1

After the update to Android 6.0 is the piece of code below not working anymore. I tried a lot of things, does someone know how to solve this problem?

I read something about the Apache HTTP Client Removal and it says that you have to add something to you build.gradle file, but i don't have this file.

public static String CallService(String url, String soapAction, String envelope)
{
    getConnection = true;

    final DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();

    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, TIMEOUT);

    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);


    HttpPost httppost = new HttpPost(url.trim()+ INHEADER_SERVER_PAGE_VALUE);
    httppost.setHeader(INHEADER_METHOD_NAME,  url + INHEADER_METHOD_VALUE);
    httppost.setHeader(INHEADER_HOST_NAME, INHEADER_HOST_VALUE);
    httppost.setHeader(INHEADER_CONTENT_TYPE_NAME, INHEADER_CONTENT_TYPE_VALUE);
    httppost.setHeader(INHEADER_USER_AGENT_NAME, INHEADER_USER_AGENT_VALUE);
    httppost.setHeader(INHEADER_SOAPACTION_NAME, soapAction);
    httppost.setHeader(INHEADER_AUTORIZATION_NAME, BASIC + authString);

    String responseString = "";
    try
    {
        String body = new String(envelope.getBytes("UTF-8"), "ISO-8859-1");
        HttpEntity entity = new StringEntity(body);
        httppost.setEntity(entity);
        ResponseHandler<String> rh = new ResponseHandler<String>(){
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException{
                HttpEntity entity = response.getEntity();

                StringBuffer out = new StringBuffer();
                byte[] data = EntityUtils.toByteArray(entity);
                out.append(new String(data, 0, data.length, "ISO-8859-1"));
                return out.toString();
            }
        };
        responseString = httpClient.execute(httppost, rh);
        return responseString;
    }
    catch(Exception e){
        e.printStackTrace();
    }
    getConnection = false;
    return "";
}
Martijn Bakker
  • 395
  • 1
  • 5
  • 15

1 Answers1

2

You can not use HttpPost or Httpclient in android 6.0. 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.

Nikhil bohra
  • 114
  • 1
  • 5