且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-04 13:25:24

使用 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” 就解决问题了。完整代码如下:

安卓 AsyncHttpClient - “Content-Type not allowed!”
            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());
                    }
                }
            });    
安卓 AsyncHttpClient - “Content-Type not allowed!”

 


本文转自 netcorner 博客园博客,原文链接: http://www.cnblogs.com/netcorner/p/4851041.html  ,如需转载请自行联系原作者