使用MultipartEntityBuilder将图片发送到Rail服务器

7
我正试图将一个MultipartEntityBuilder发送到我的Rails服务器。但是,当我尝试构建它时,它会崩溃并给出以下错误:

03-25 09:44:50.001 W/System.err﹕ java.util.concurrent.ExecutionException: java.lang.NoSuchMethodError: No static method create(Ljava/lang/String;[Lorg/apache/http/NameValuePair;)Lorg/apache/http/entity/ContentType; in class Lorg/apache/http/entity/ContentType; or its super classes (declaration of 'org.apache.http.entity.ContentType

        HttpPost httpost = new HttpPost(url);
        MultipartEntityBuilder entity = new MultipartEntityBuilder.create();
        entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        for(int index=0; index < nameValuePairs.size(); index++) { 
            ContentBody cb;
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("File")) { 
                File file = new File(nameValuePairs.get(index).getValue());
                FileBody isb = new FileBody(file);
                entity.addPart(nameValuePairs.get(index).getName(), isb);
            } else { 
                // Normal string data 
                                    
                cb =  new StringBody(nameValuePairs.get(index).getValue(),ContentType.TEXT_PLAIN);
                entity.addPart(nameValuePairs.get(index).getName(),cb); 
            } 
        } 
 return entity.build();

这是我正在使用的代码,但在构建MultipartEntity时仍然会出现错误。

1个回答

23

尝试使用httpmime 4.3.6版本。

我尝试使用4.4+,但问题始终存在。


在Android Studio中,使用文件|项目结构|依赖项|+按钮,不要使用使用Maven的库依赖项,这会添加最新的httmime。相反,按照@Matheus的建议下载4.3.6版本并将其添加为文件依赖项。然后,您的build.gradle将包含一行类似于compile files('libs/httpmime-4.3.6.jar')的内容。 请记住,您可能需要再次在Android Studio中与Gradle文件同步您的项目。 - n00b
2
或者只需将compile 'org.apache.httpcomponents:httpmime:4.3.6'放在您的build.gradle文件的dependencies中。 - Matheus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接