Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I'm now trying to call a web service that streams files back to the client over HTTP. I'm therefore using the BinaryHttpResponseHandler to capture the byte[] data returned. 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! '.

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. 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.

like image 559
Jonathan Wareham Avatar asked Dec 26 '22 18:12

Jonathan Wareham


2 Answers

Ignore me, there is nothing wrong with 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. However I used WireShark to inspect the HTTP response headers coming back and found the content type was actually 'text/html; charset=ISO-8859-1'. Once I added this to the allowed content types everything worked fine.

like image 67
Jonathan Wareham Avatar answered Apr 08 '23 20:04

Jonathan Wareham


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"

then add it ;)

like image 44
javamatic Avatar answered Apr 08 '23 20:04

javamatic