Request request = new Request.Builder()
.url(url)
.addHeader("Cookie", "JSESSIONID="+sessionId)
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
InputStream inputStream = response.body().byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageView.setImageBitmap(bitmap);
}
我使用okhttp3來建立一個cookie和session的請求,並且response.body()驗證代碼。 inputStream不爲空,但位圖爲空。我嘗試了下面的方式,但位圖也是空的。如何使用okhttp將驗證碼添加到imageView?
private Bitmap convertBitmap(final Response response) throws IOException {
InputStream inputStream = response.body().byteStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
byte[] bmp_buffer;
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.close();
inputStream.close();
bmp_buffer = outStream.toByteArray();
return BitmapFactory.decodeByteArray(bmp_buffer, 0, bmp_buffer.length);
}
您確定響應中的byteArray是圖片嗎? –
是的。我已經使用Glide來嘗試網址,它可以顯示在imageView –