簡體   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