由于HTTP客户端相关的类,我有大量的弃用。如何修复使用原生Java库多个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
,但我还没有理解如何将我的代码迁移到这些库。
这是一个/意见为基础的B /不正确 – njzk2