我使用HttpClient和httpost上传我的图像文件以及一些参数。
我的代码看起来像
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("xyz.com");
ArrayList<NameValuePair> postParameters;
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name","Temp"));
postParameters.add(new BasicNameValuePair("id","12345"));
httpost.setEntity(new UrlEncodedFormEntity(postParameters));
MultipartEntity entity = new MultipartEntity();
File imgFile = new File("C:\test.img");
FileBody imgFileBody = new FileBody(imgFile);
entity.addPart("multipartcontent", imgFileBody); //No i18n
httpost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpost);
我没有在服务器中获取param值。我做错了什么。请指导我。
答案 0 :(得分:0)
httpost.setEntity(new UrlEncodedFormEntity(postParameters));
...
httpost.setEntity(entity);
多部分实体会覆盖已编码的URL,完全丢弃其内容。
您应该将param值作为一个或多个正文部分添加到多部分实体