[英]Android AsyncHttpClient download image file with Content-Type: unkonwn header
I'm using the AsyncHttpClient library from http://loopj.com/android-async-http/ and have it calling web services fine to retrieve JSON responses. 我正在使用来自http://loopj.com/android-async-http/的AsyncHttpClient库,让它可以调用Web服务来检索JSON响应。 I'm now trying to call a web service that streams files back to the client over HTTP.
我现在正试图调用一个Web服务,通过HTTP将文件流回客户端。 I'm therefore using the BinaryHttpResponseHandler to capture the byte[] data returned.
因此我使用BinaryHttpResponseHandler来捕获返回的byte []数据。 The code I'm using:
我正在使用的代码:
public static void getImage(Context context, String url, final ImageLoadingListener listener) {
boolean online = isOnline(context);
client = new AsyncHttpClient();
if (online) {
String[] allowedContentTypes = new String[]{"image/png", "image/jpeg"};
client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
client.get(Html.fromHtml(url).toString(), new BinaryHttpResponseHandler(allowedContentTypes) {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
listener.imageReceived(bmp);
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
printRequestResult(i, headers);
}
});
}
}
However when I try to download image file with header Content-Type: set as "unknown" it fails, and when examining the Throwable object the exception is 'org.apache.http.client.HttpResponseException: Content-Type not allowed! 但是当我尝试下载带有标题Content-Type的图像文件时:设置为“unknown”它失败了,当检查Throwable对象时,异常是'org.apache.http.client.HttpResponseException:不允许Content-Type! '.
”。
Image I try to download is: http://media.zenfs.com/en_us/News/Engadget/opHNMlIEDUj72LqIRjgU66Z9ddGI6_GjULpJJpg0iuQSUUUNT-wpyKA5LlWCaHw09N_KlKFNU55j81P6QLhOYwScbeX0ABeo_SL1cmNn4KEHqfL8of0dGpp1h9_2ZbgkT1PYJGuil5bt2uyYbpyXYDdCcCtFmm9Vdmvnd93dEwVY9DygmaXajJcYNj1vwPg-fbQaTqFe6Dv5GKmTm-PRaJNTzFzfOTzLDRYvDH1nQEk 图片我尝试下载的是: http://media.zenfs.com/en_us/News/Engadget/opHNMlIEDUj72LqIRjgU66Z9ddGI6_GjULpJJpg0iuQSUUUNT-wpyKA5LlWCaHw09N_KlKFNU55j81P6QLhOYwScbeX0ABeo_SL1cmNn4KEHqfL8of0dGpp1h9_2ZbgkT1PYJGuil5bt2uyYbpyXYDdCcCtFmm9Vdmvnd93dEwVY9DygmaXajJcYNj1vwPg-fbQaTqFe6Dv5GKmTm-PRaJNTzFzfOTzLDRYvDH1nQEk
If it is properly displayed in browser, is there any way to download it using AsyncHttpClient library? 如果它在浏览器中正确显示,有没有办法使用AsyncHttpClient库下载它?
I manage to come round Content-type check by specifying: 我设法通过指定以下内容来进行内容类型检查:
String[] allowedContentTypes = new String[]{".*"};
I guess it is not the best solution, cause it just turns out Content-type check, but this is the best I could come up with 我想这不是最好的解决方案,因为它只是内容类型检查,但这是我能想到的最好的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.