由于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
,但我还不明白如何将我的代码迁移到这些库
如何解决使用本地库的问题?
我不知道在这种情况下你认为"本地库"是什么。Android内置的HttpClient版本在Android 5.1中被弃用,并且在M开发者预览版中被完全删除。
如果你需要坚持使用Apache的HttpClient API,切换到Apache的版本。
或者,切换到任意数量的其他HTTP api,如内置的HttpUrlConnection
, OkHttp等。
这种大规模弃用的原因是什么?
Google几年前就指出你不应该使用HttpClient;
我已经构建了一个使用标准Java库的HttpUtil类。您也可以修改它。我只是把所有东西都设置为map,然后你可以很容易地发送Post或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;
}
}
正如Google文档所说,您仍然可以使用HTTP Apache API接口。在gradle中声明以下依赖项。构建文件:
android {
useLibrary 'org.apache.http.legacy'
}
如果你正在面向Android开发游戏,你应该已经转向Volley,这是更好的选择。这些方法已被弃用,而且据我所知没有其他的本机库。