java 找不到从方法引用的类“org.apache.http.entity.mime.content.Filebody”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16671939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:37:19  来源:igfitidea点击:

Could not find class 'org.apache.http.entity.mime.content.Filebody', referenced from method

javaandroiderror-handlingjar

提问by Mat

What I'm trying to do is to send a picture to a web server. When I call a method in my Android project I get the following error: could not find class 'org.apache.http.entity.mime.content.Filebody', referenced from method com.example.tc.Send.send.

我想要做的是将图片发送到网络服务器。当我在我的 Android 项目中调用一个方法时,我收到以下错误:找不到类 'org.apache.http.entity.mime.content.Filebody',从方法 com.example.tc.Send.send 引用。

This happens eventhough I've got the following imports:

即使我有以下导入,也会发生这种情况:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

This is what the class looks like in which the method lies:

这是方法所在的类的样子:

public class Send {
public Send(){
}

public static String send(String path) throws Exception {
    String filePath = path;
    String svar;

    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost httppost = new HttpPost("path to web server"); 
        FileBody pic = new FileBody(new File(filePath)); 
        MultipartEntity requestEntity = new MultipartEntity(); 
        requestEntity.addPart("file", pic);

        httppost.setEntity(requestEntity);
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());

        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        response.getEntity().writeTo(outstream);
        byte [] responseBody = outstream.toByteArray();
        svar = new String(responseBody);
        System.out.println(svar);

    } finally {
        try {
            httpclient.getConnectionManager().shutdown();
        } 
        catch (Exception ignore) {
        }
    }
    return svar;
}
}

Can anyone see what the problem is?

任何人都可以看到问题是什么?

回答by Sanjay Jain

Add these two dependency

添加这两个依赖

compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"

回答by Alexandros

You need to have httpmime-4.0.jar in your classpath. You may (i guess you do) have this jar into your project, but is it inside your application during runtime? If i understand it correctly you get this from the android application. You need to have this jar inthe classpath of your android.

您的类路径中需要有 httpmime-4.0.jar。您可能(我猜您有)将这个 jar 放入您的项目中,但是在运行时它是否在您的应用程序中?如果我理解正确,您可以从 android 应用程序中获得此信息。你需要在你的 android 的类路径中有这个 jar。

回答by Srinivasan Sekar

Download Apache HttpComponents jar file from this link http://hc.apache.org/downloads.cgiand create a /libs directory within your Android project directory and copy the JAR file to that directory. add it to your project by Clicking on the Project properties for your Android project and then select Java Build Path settings, select the Libraries tab and click Add JAR button and choose the jar within the /libs directory.

从此链接http://hc.apache.org/downloads.cgi下载 Apache HttpComponents jar 文件, 并在您的 Android 项目目录中创建一个 /libs 目录,并将 JAR 文件复制到该目录。通过单击 Android 项目的项目属性,然后选择 Java 构建路径设置,选择库选项卡并单击添加 JAR 按钮并选择 /libs 目录中的 jar,将其添加到您的项目中。

回答by Mick

You can also get these files from the Eclipse ADT bundle without having to download them separately - see the answer here:

您还可以从 Eclipse ADT 包中获取这些文件,而无需单独下载它们 - 请参阅此处的答案:

https://stackoverflow.com/a/27906193/334402

https://stackoverflow.com/a/27906193/334402

Note that you will get the error the original poster flagged, i.e. 'Could not find class 'org.apache.http.entity.mime.content.Filebody', if you just add the external JAR files to your build path but forget to import the archive into your project also.

请注意,如果您只是将外部 JAR 文件添加到构建路径但忘记导入,则会收到原始海报标记的错误,即“找不到类 org.apache.http.entity.mime.content.Filebody”也将存档文件放入您的项目中。

回答by Krishna sheladiya

  • Add dependency :

    compile 'org.apache.httpcomponents:httpcore:4.4.4'
  • 添加依赖:

    compile 'org.apache.httpcomponents:httpcore:4.4.4'