问题描述
我尝试包括httpmime在使用build.gradle文件我的应用程序,一切编译罚款。相反,当应用程序试图实际使用MultipartEntityBuilder类,还有一堆的WARN在日志中说,有问题级的消息。
I try to include httpmime in my application using the build.gradle file, and everything compiles fine. Instead, when the application tries to actually use the MultipartEntityBuilder class, there are a bunch of WARN level messages on the log saying that there are problems.
下面是从我的build.gradle的依赖的摘录:
Here's the excerpt from my build.gradle for the dependency:
compile('org.apache.httpcomponents:httpmime:4.+') {
exclude module: "httpclient"
}
下面是错误的:
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 6967 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 6967 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 6967 (DEFAULT_BINARY) in Lorg/apache/http/entity/ContentType;
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.367 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.377 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to resolve static method 19478: Lorg/apache/http/util/Args;.notNull (Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
10-09 13:39:37.377 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 6968 (DEFAULT_TEXT) in Lorg/apache/http/entity/ContentType;
10-09 13:39:37.377 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
10-09 13:39:37.377 2409-2426/com.company.app W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lorg/apache/http/entity/ContentType;)
Java类:
The java class:
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
public class FileUploader {
private final static String BOUNDARY = "__--__--__SERVETHEOVERMIND-__-_";
public void uploadFile(String targetUrl, MultipartEntityBuilder upload, UploadHandler after) {
Log.v("FileUploader", "Uploading to " + targetUrl);
HttpURLConnection con = null;
OutputStream os = null;
InputStream is = null;
try {
HttpEntity uploadEntity = upload.build();
URL postTo = new URL(targetUrl);
con = (HttpURLConnection) postTo.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.addRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-length", String.valueOf(uploadEntity.getContentLength()));
os = con.getOutputStream();
uploadEntity.writeTo(os);
os.close();
con.connect();
is = con.getInputStream();
after.consumeUploadResponse(is);
con.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
if(con != null) {
con.disconnect();
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
Log.v("Uploader", "Closed output stream");
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
Log.v("Uploader", "Closed input stream");
}
}
}
public interface UploadHandler {
public void consumeUploadResponse(InputStream stream);
}
}
正确的依赖关系,因为每个答案
compile('org.apache.httpcomponents:httpmime:4.+') {
exclude module: "httpclient"
}
compile('org.apache.httpcomponents:httpcore:4.+') {
exclude module: "httpclient"
}
[第二编辑]但问题仍然存在 - 现在是这些失踪位,但它可能是在后端的问题:
10-10 11:51:54.998 29597-29638/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 7465 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueParser;
10-10 11:51:54.998 29597-29638/com.company.app W/dalvikvm﹕ VFY: unable to resolve static field 7459 (INSTANCE) in Lorg/apache/http/message/BasicHeaderValueFormatter;
[又一编辑]
看来最后的小缺失位上没有在这种情况下成功使用MultipartEntityBuilder的任何效果。
It seems the last little missing bits don't have any effect on the successful use of the MultipartEntityBuilder in this case.
推荐答案
您需要的HttpCore-4.3.jar添加到您的Java构建路径。我有同样的问题,它的消失将这个jar后。
You need to add httpcore-4.3.jar to your java build path. I had the same problem and it's gone after adding this jar.
这篇关于存在的问题包括在Android的摇篮项目的Apache HttpComponents的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!