简体   繁体   English

Android AsyncHttpClient下载包含Content-Type:unkonwn标头的图像文件

[英]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.

相关问题 下载文件时,Android AsyncHttpClient - “内容类型不允许!” - Android AsyncHttpClient - “Content-Type not allowed!” when downloading files Android AsyncHttpClient-“不允许使用Content-Type!”不管提供的类型如何 - Android AsyncHttpClient - “Content-Type not allowed!” Regardless of types provided 在Android上从浏览器下载具有特定内容类型的文件时,如何打开我的应用程序? - How to open my app when I download a file with specific content-type from the browser, on android? Android通过MultipartEntity将图像发送到服务器 - 设置内容类型? - Android sending image to Server via MultipartEntity - setting Content-type? 在浏览器中下载链接/如果标题中有特殊的Content-Type,请打开我的活动 - Download link in browser / Open my activity if there is a special Content-Type in the Header Volley Content-Type标头未更新 - Volley Content-Type header not updating Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header 如何使用 Android 中的 AsyncHttpClient 从 Excel 文件下载和读取 - How to download and read from an Excel file using AsyncHttpClient in Android Android WebView:当内容类型为 application/pdf 时下载,如果不是则呈现网页 - Android WebView: Download when content-type is application/pdf and render web page if not 如何在Soap Web服务中添加“内容类型”标头 - How to add “content-type” header in soap webservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM