Java 如何在androidhttppost中使用MultipartEntity

Java 如何在androidhttppost中使用MultipartEntity,java,android,spring,Java,Android,Spring,我怀疑多党制。 首先,它是否被弃用。 第二,如何在我的项目中导入MultipartEntity。 我确实将apachehttpclient-4.4.1、httpcore-4.4.1、httpmime-4.4.1中的jar添加到了我的项目libs文件夹中。 但我没有使用多方当事人在我身边有任何错误请帮助我? 我想将图像从android上传到spring控制器 Android代码是: HttpClient client = new DefaultHttpClient(); HttpC

我怀疑多党制。 首先,它是否被弃用。 第二,如何在我的项目中导入MultipartEntity。 我确实将apachehttpclient-4.4.1、httpcore-4.4.1、httpmime-4.4.1中的jar添加到了我的项目libs文件夹中。 但我没有使用多方当事人在我身边有任何错误请帮助我? 我想将图像从android上传到spring控制器

Android代码是:

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("userMO", jsonUserMo));
        HttpPost post = new HttpPost(Constants.ROOTURL+"/media/uploadUserImage");
        post.setHeader("Content-type", "multipart/form-data; boundary=***");
        post.setEntity(new FileEntity(profileImage,"image/jpeg"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        contactLists.append(rd.readLine());
    } catch (Exception e) {
        e.printStackTrace();
    }
android http status 400 RequiredMultipartFile参数“uploadImg”不存在时出错 如何解决这个问题

try {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("type", new StringBody("uploadImg"));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
}

您应该将addPart属性与多部分实体一起添加,以便获得servlet在服务器端所期望的附加参数。在您的情况下,uploadImg是服务器在您的请求中期望的附加参数。希望它能解决您的问题。

无法使用MultipartEntityBuilder?我遇到了同样的问题,并在下面的URL中找到了解决方案。该答案缺乏解释或内容,无法说明这是一个解决方案。读者很难单独用这个片段来解决问题。考虑添加解释和记录解决方案是什么,所以你的答案可能是有益的,值得吸引更多的选票。
try {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("type", new StringBody("uploadImg"));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
}