[英]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: 只需覆盖
BinaryHttpResponseHandler
的onFailure
,如下所示:
@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.