用 httpclient 上传文件 和 普通参数 时服务器返回400
public void run() {
    //TelephonyManager telephonyManager= (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    try {
        HttpClient clicent = new DefaultHttpClient();
        HttpPost post = new HttpPost(actionUrl);
        //FileBody fileBody = new FileBody(files.get("img"));
        //用这里的代码服务器返回400错误
        //MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        //entity.addPart("cId",new StringBody("11",Charset.forName(HTTP.UTF_8)));
        //entity.addPart("img",new FileBody(files.get("img")));

        List<NameValuePair> params= new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("cId","111"));
        post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));

        //post.setEntity(entity);
        HttpResponse httpResponse = clicent.execute(post);
        if (httpResponse.getStatusLine().getStatusCode()==200) {
            System.out.println(httpResponse.getStatusLine().getStatusCode());
        }
        System.out.println(httpResponse.getStatusLine().getStatusCode());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问