简体   繁体   English

下载文件时,Android AsyncHttpClient - “内容类型不允许!”

[英]Android AsyncHttpClient - “Content-Type not allowed!” when downloading files

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 []数据。 However every time I try to call the method it fails, and when examining the Throwable object the exception is 'org.apache.http.client.HttpResponseException: Content-Type not allowed! 但是,每当我尝试调用该方法时它都会失败,并且在检查Throwable对象时,异常是'org.apache.http.client.HttpResponseException:不允许Content-Type! '. ”。

I've tried specifying a list of content types to allow as per the docs, but this hasn't made a difference. 我已经尝试根据文档指定允许的内容类型列表,但这并没有什么不同。 I'm mostly streaming PDFs but ideally I don't want to specify a list of content types, I want to be able to download any file type. 我主要是流式传输PDF,但理想情况下我不想指定内容类型列表,我希望能够下载任何文件类型。 The code I'm using is as follows : 我正在使用的代码如下:

AsyncHttpClient httpClient = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "application/pdf", "image/png", "image/jpeg" };
httpClient.get(myWebServiceURL, new BinaryHttpResponseHandler(allowedContentTypes) {
    @Override
    public void onSuccess(byte[] binaryData) {
        // ....
    }
    @Override
    public void onFailure(Throwable error, byte[] binaryData) {
        // ....
        Log.e("Download-onFailure", error.getMessage()); 
    }
});

I've also tried not specifying any content types, just using : 我还尝试不指定任何内容类型,只使用:

new BinaryHttpResponseHandler() 

but this made no difference. 但这并没有什么区别。

Ignore me, there is nothing wrong with BinaryHttpResponseHandler. 忽略我,BinaryHttpResponseHandler没有任何问题。 The files I'm pulling from the web service are PDF, JPG, PNG etc so I had allowed content types of application/pdf, image/jpeg, image/png. 我从Web服务中提取的文件是PDF,JPG,PNG等,因此我允许使用application / pdf,image / jpeg,image / png的内容类型。 However I used WireShark to inspect the HTTP response headers coming back and found the content type was actually 'text/html; 但是我使用WireShark来检查返回的HTTP响应标头,发现内容类型实际上是'text / html; charset=ISO-8859-1'. 字符集= ISO-8859-1' 。 Once I added this to the allowed content types everything worked fine. 一旦我将其添加到允许的内容类型,一切正常。

add following method to see the "not accepted" content 添加以下方法以查看“未接受”内容

public void sendResponseMessage(HttpResponse response) {
    System.out.println(response.getHeaders("Content-Type")[0].getValue());
}

for me was the result "image/png;charset=UTF-8" 对我来说是结果“image / png; charset = UTF-8”

then add it ;) 然后添加它;)

I found the code in BinaryHttpResponseHandler.java follows: 我发现BinaryHttpResponseHandler.java的代码如下:

boolean foundAllowedContentType = false;
for(String anAllowedContentType : mAllowedContentTypes) {
    if(anAllowedContentType.equals(contentTypeHeader.getValue())) {
        foundAllowedContentType = true;
    }
}

It seems that you must lists all kind of types you want to receives. 您似乎必须列出您想要接收的所有类型。

You can check exactly what kind of file is returned by your web services. 您可以确切地检查Web服务返回的文件类型。 Just overwrite the onFailure in your BinaryHttpResponseHandler like this: 只需覆盖BinaryHttpResponseHandleronFailure ,如下所示:

@Override 
public void onFailure(int statusCode, Header[] headers, byte[] binaryData, Throwable error) 
{ 
    Log.e(TAG, "onFailure!"+ error.getMessage());
    for (Header header : headers)
    {
        Log.i(TAG, header.getName()+" / "+header.getValue());
    }
}   

Hope this helps 希望这可以帮助

尝试添加*/*

String[] allowedContentTypes = new String[] { "*/*", "application/pdf", "image/png", "image/jpeg" };

Adding "application/octet-stream" as a allowed type worked for me! 添加“application / octet-stream”作为允许类型对我有用!

Cheers 干杯

I encounted a same problem.I checked the source. 我遇到了同样的问题。我检查了来源。 URL is following 网址如下

https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/BinaryHttpResponseHandler.java https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/BinaryHttpResponseHandler.java

android-async only support two Content-Type:"image/jpeg","image/png"。 android-async只支持两个Content-Type:“image / jpeg”,“image / png”。

I think if you need Content-Type is others,you need override the class. 我想如果你需要Content-Type是其他的,你需要覆盖这个类。

just do like this: 就像这样:

String[] allowedContentTypes = new String[] { "image/jpeg;charset=utf-8", "image/jpeg;charset=utf-8" };

it's ok. 没关系。

Had same problem. 有同样的问题。 After digging a while came up with solution to add ".*" at the end of content types in order to prevent specifying all combinations of actual content-types and charsets: 在挖掘了一段时间之后,提出了在内容类型末尾添加“。*”的解决方案,以防止指定实际内容类型和字符集的所有组合:

String[] allowedContentTypes = new String[] { "application/pdf.*", "image/png.*", "image/jpeg.*" };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android AsyncHttpClient-“不允许使用Content-Type!”不管提供的类型如何 - Android AsyncHttpClient - “Content-Type not allowed!” Regardless of types provided Android AsyncHttpClient下载包含Content-Type:unkonwn标头的图像文件 - Android AsyncHttpClient download image file with Content-Type: unkonwn header Access-Control-Allow-Headers 不允许 IONIC Content-Type - IONIC Content-Type is not allowed by Access-Control-Allow-Headers 在Android上通过浏览器下载文件时避免内容类型问题 - Avoiding content type issues when downloading a file via browser on Android 当Content-Type = application / octet-stream时,Android如何处理改造响应? - Android how to handle retrofit response when Content-Type = application/octet-stream? 在Android上从浏览器下载具有特定内容类型的文件时,如何打开我的应用程序? - How to open my app when I download a file with specific content-type from the browser, on android? Android WebView:当内容类型为 application/pdf 时下载,如果不是则呈现网页 - Android WebView: Download when content-type is application/pdf and render web page if not Android Pixel 2发送了错误的Content-Type或内容 - Android Pixel 2 sending incorrect Content-Type or content 在通过毕加索下载图像之前,如何通过Content-Length和Content-Type验证响应头? - How to validate response headers by Content-Length and Content-Type before downloading an image via Picasso? Android通过MultipartEntity将图像发送到服务器 - 设置内容类型? - Android sending image to Server via MultipartEntity - setting Content-type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM