8

我尝试在我的应用程序中使用build.gradle文件包含httpmime,并且所有的编译都很好。相反,当应用程序尝试实际使用MultipartEntityBuilder类时,日志中会有一堆警告级别的消息,说明存在问题。包括Android Gradle项目中的Apache HttpComponents在内的问题

下面是从我的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没有任何影响。

+0

您是否设法删除了Lorg/apache/http/message ....静态字段警告?我似乎现在有类似的问题。 –

+0

上面的第一个代码块修复了我在android studio中收到的警告。谢谢! – Josh

回答

8

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

+0

我以为是的,但我仍然陷在类似的问题 –

+0

它看起来像它可能工作,尽管最新的错误信息 - 现在我正在看轨道后端 –

+0

如果你是新的Android Studio和现在确定如何将jar添加到你的构建路径中,看到类似问题的答案:http://stackoverflow.com/a/28712564 - 你只需要从'httpcore'而不是'httpmime'进行搜索。 –

13

这是我在gradle这个是怎么做..

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' 
} 
} 
0
compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1' compile('org.apache.httpcomponents:httpmime:4.3') { 
    exclude module: "httpclient" } 

您可以使用此以上依赖于的build.gradle(模块:应用)到您的项目以下IMPORT语句

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