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

Jonathan Wareham picture Jonathan Wareham · Sep 12, 2012 · Viewed 8.9k times · Source

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.

Answer

Jonathan Wareham picture Jonathan Wareham · Sep 17, 2012

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.