由於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