2015年6月2日星期二

使用Http 上传文件出错解决(java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE)

在 app gradle文件里 添加
compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
即可



//下面是上传文件. 可复制
public MultiPartRequest.ResponseResult httpUploadStringResult(String url,List<NameValuePair> params, List<NameValuePair> filePaths) throws Exception {

        String mCharSet = null;
        String mMimeType = null;
        MultiPartRequest multiPartRequest=new MultiPartRequest();
        MultiPartRequest.ResponseResult<String> responseResult=multiPartRequest.new ResponseResult();
        // 定义HttpClient对象
                HttpClient client = mHttpClient;

        HttpPost post = new HttpPost(HttpLoader.getServerUrl(url));
// post.addHeader("charset", HTTP.UTF_8);
        post.addHeader(
                "User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; WOW64; SV1; .NET CLR 2.0.50727)");
        post.addHeader("Accept",
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

//        MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        MultipartEntityBuilder me=MultipartEntityBuilder.create();
        if (params!=null){
            for (int i = 0; i < params.size(); i++) {
                me.addPart(params.get(i).getName(), new StringBody(params
                        .get(i).getValue(), Charset.forName(HTTP.UTF_8)));
            }
        }

        if(filePaths!=null){
            if (filePaths.size()>0) {
                String[] userimg=new String[filePaths.size()];
                for (int i = 0; i < filePaths.size(); i++) {
                    userimg[i]=filePaths.get(i).getName();
                }
                for (int j = 0; j < filePaths.size(); j++) {
                    File tmpFile = new File(filePaths.get(j).getValue());
                    if (tmpFile != null && tmpFile.exists())
// me.addPart(filePaths.get(j).getName(), new InputStreamBody(
// new FileInputStream(tmpFile),
// "application/octet-stream", tmpFile.getName()));
                        me.addPart(userimg[j], new FileBody(tmpFile));
                }
            }
        }
        post.setEntity(me.build());
//        post.setEntity(me);
        HttpResponse resp = null;

        resp = client.execute(post);

        if (resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity tmpHttpEntity = resp.getEntity();
            mCharSet = EntityUtils.getContentCharSet(tmpHttpEntity);
            Header tmpContentType = tmpHttpEntity.getContentType();
            mMimeType = null;
            if (tmpContentType != null) {
                HeaderElement[] tmpContentTypes = tmpContentType
                        .getElements();
                for (int i = 0; i < tmpContentTypes.length; i++) {
                    mMimeType = tmpContentTypes[i].getName();
                }
            }
            if (mCharSet == null)
                responseResult.resultDate=EntityUtils.toString(resp.getEntity());
            else
                responseResult.resultDate=EntityUtils.toString(resp.getEntity(), mCharSet);
        }
        responseResult.statusCode=resp.getStatusLine().getStatusCode();

        return responseResult;
    }

没有评论:

发表评论