由于与 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
,但我还不明白如何将我的代码迁移到这些库中。