httpclient 3から4への変換
-
15-10-2019 - |
質問
私はなんとかすべての変更を加えることができましたが、次のことを示しました。
HttpClient client;
HttpPost method;
client = new DefaultHttpClient();
method = new HttpPost(url);
InputStream rstream;
try {
rstream = method.getResponseBodyAsStream();
} catch (IOException e) {
return BadSpot(e.getMessage());
}
私が確信していないのは、getResponseBodyAsstream()を置き換える必要があることです。
解決
InputStream rstream;
try {
HttpResponse response = client.execute(HttpHost, method);
rstream = response.getEntity().getContent();
} catch (IOException e) {
return BadSpot(e.getMessage());
}
上記はあなたが求めていることをする必要があります。
他のヒント
UTILクラスにはいくつかの役立つ方法があります。
EntityUtils.toString(response.getEntity());
例にはいくつかの有用なものもあります Apacheのウェブサイト
使用する EntityUtils
返されたエンティティを確認してください not null
エンティティを消費する前:
InputStream rstream;
try {
HttpResponse response = client.execute(HttpHost, method);
rstream = Optional.ofNullable(httpResponse.getEntity())
.map(e -> response.getContent()).orElse(null);
} catch (IOException e) {
return BadSpot(e.getMessage());
}
ノート: ここの入力ストリームは無効である可能性があり、何よりも、実際に応答を閉じる/接続をリリースする前に消費されることを確認する必要があります。
所属していません StackOverflow