Android AsyncHttpClient - "Content-Type not allowed!" when downloading files

I use the AsyncHttpClient library from http://loopj.com/android-async-http/ and force it to access web services correctly to receive JSON responses. Now I am trying to call a web service that transfers files back to the client via HTTP. Therefore, I use BinaryHttpResponseHandler to capture the returned byte [] data. However, every time I try to call a method, it fails, and when examining the Throwable object, the exception is "org.apache.http.client.HttpResponseException: Content-Type is not allowed!".

I tried to specify a list of content types allowed according to the documents, but this did not change the situation. I am mainly streaming PDF files, but ideally I do not want to specify a list of content types, I want to be able to download any type of file. The code I use 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 also tried not to specify any types of content, just using:

 new BinaryHttpResponseHandler() 

but it didn’t matter.

+4
source share
9 answers

Ignore me, there is nothing wrong with the BinaryHttpResponseHandler. The files that I extract from the web service are PDF, JPG, PNG, etc., so I allowed the application content types / pdf, image / jpeg, image / png. However, I used WireShark to check the returned headers of the HTTP responses and found that the content type was actually 'text / html; encoding = ISO-8859-1'. As soon as I added this to the allowed content types, everything worked fine.

+6
source

add the following method to see "unacceptable" content

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

for me there was the result "image / png; charset = UTF-8"

then add it;)

+2
source

I found the code in BinaryHttpResponseHandler.java :

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

It seems that you should list all the types of types you want to get.

+1
source

You can check exactly which file is returned by your web services. Just onFailure in your BinaryHttpResponseHandler as follows:

 @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

+1
source

Try adding */*

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

Adding "application / octet-stream" as the allowed type for me!

Greetings

0
source

I found the same problem. I checked the source. Url next

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

android-async only supports two Content-Type: "image / jpeg", "image / png".

I think if you need Content-Type - these are different, you need to override the class.

0
source

just follow these steps:

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

this is normal.

0
source

There was the same problem. After digging at that time, there was a decision to add “. *” At the end of the content types to prevent all combinations of actual content types and encodings from being indicated:

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

Source: https://habr.com/ru/post/1433784/


All Articles