如何从HttpResponse读取bytearray?

Roz*_*ozy 4 android

我正在使用以下代码建立http连接:

HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = client.execute(httpPost);
Run Code Online (Sandbox Code Playgroud)

我想从响应对象中读取一个bytearray.

我该怎么办?

san*_*den 5

您可以使用以下方法直接读取字节数组:EntityUtils.toByteArray

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("localhost:8080/myurl");
HttpResponse response = client.execute(get);
byte[] content = EntityUtils.toByteArray(response.getEntity());
Run Code Online (Sandbox Code Playgroud)