繁体   English   中英

问题包括Android Gradle项目中的Apache HttpComponents

[英]Problems including Apache HttpComponents in Android Gradle project

我尝试使用build.gradle文件在我的应用程序中包含httpmime,所有内容编译都很好。 相反,当应用程序尝试实际使用MultipartEntityBuilder类时,日志上有一堆WARN级别消息说有问题。

以下是我的build.gradle对依赖项的摘录:

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类:

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没有任何影响。

这就是我在学习中的表现。

dependencies { 
compile ('org.apache.httpcomponents:httpmime:4.3'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile ('org.apache.httpcomponents:httpcore:4.4.1'){
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'

}
}

而在android内部

android{
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
}
}

您需要将httpcore-4.3.jar添加到您的java构建路径中。 我有同样的问题,添加这个jar后它就消失了。

 compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'  compile('org.apache.httpcomponents:httpmime:4.3') {
  exclude module: "httpclient"  }

对于以下IMPORT语句,您可以将以上依赖项用于build.gradle (Module:app)到您的Project

import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MIME;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM