安卓 AsyncHttpClient - “Content-Type not allowed!”

使用 http://loopj.com/android-async-http/ 的异步网络请求工具使用BinaryHttpResponseHandler来完成文件下载时总是报错,“org.apache.http.client.HttpResponseException: Content-Type not allowed!”。若缺少某个请求的类型,比如我下载一个jpg文件,只要加上“image/jpg;charset=UTF-8” ,若对于html文件后来加上“text/html;charset=UTF-8” 就解决问题了。完整代码如下:

            String[] allowedContentTypes = new String[] { "text/html;charset=UTF-8", "image/jpg;charset=UTF-8"};
            String urlStr="your request url";
            HttpUtil.get(urlStr, new BinaryHttpResponseHandler(allowedContentTypes) {
                public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
                    Log.d("myingwill","ok");
                    
                }
                public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, java.lang.Throwable throwable) {
                    Log.d("myingwill",throwable.toString());
                    for (org.apache.http.Header header : headers)
                    {
                        Log.i("myingwill", header.getName()+" / "+header.getValue());
                    }
                }
            });    

 

posted @ 2015-10-01 15:16  netcorner  阅读(1111)  评论(0编辑  收藏  举报