由于与http客户端相关的类,我有大量的弃用。
这是我的代码:
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
已弃用
HttpResponse
已弃用
HttpEntity
已弃用
我如何修复使用本机库?我搜索了一些人HttpClient
使用HttpClientBuilder
但需要额外的库,此外我不知道如何解决其他弃用问题。
有些程序员可以帮助我吗? 这种大规模弃用的原因是什么?
现在我们应该使用HttpURLConnection
,但我还不了解如何将代码迁移到这些库。
答案 0 :(得分:0)
我如何使用本机库修复?
我不知道你认为“本土图书馆”在这种情况下是什么。 Android的内置版HttpClient已被Android 5.1弃用,并且已在M Developer Preview中完全删除。
如果您需要坚持使用Apache HttpClient API,请切换到Apache's edition of their library。
或者,切换到任意数量的其他HTTP API,例如内置的HttpUrlConnection
,OkHttp等。
这种大规模弃用的原因是什么?
Google has indicated for a few years that you should not use HttpClient;他们现在只是强制执行。
答案 1 :(得分:0)
我构建了一个使用标准Java库的HttpUtil类。你可以修改它。我只是将所有内容设置为地图,然后您可以轻松发送帖子或获取。
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;
}
}
答案 2 :(得分:0)
Google documentation表示您仍然可以使用HTTP Apache API接口。在gradle.build文件中声明以下依赖项:
android {
useLibrary 'org.apache.http.legacy'
}
答案 3 :(得分:-1)
如果你正在为Android开发,你应该转移到Volley,这要好得多。这些方法已被弃用,我记得没有其他本地库。