更新到Android 6.0后,下面的代码不再起作用了。我尝试了很多东西,有人知道如何解决这个问题吗?
我读了一些关于Apache HTTP Client Removal的内容,它说你必须在build.gradle文件中添加一些内容,但是我没有这个文件。
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 "";
}
答案 0 :(得分:2)
你不能在android 6.0中使用HttpPost或Httpclient。 Android 6.0版本删除了对Apache HTTP客户端的支持。如果您的应用使用此客户端并定位到Android 2.3(API级别9)或更高版本,请改用HttpURLConnection类。