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.
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.
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With