【问题标题】:Could not find class 'org.apache.http.entity.mime.content.Filebody', referenced from method找不到从方法引用的类 'org.apache.http.entity.mime.content.Filebody'
【发布时间】:2013-05-16 08:01:37
【问题描述】:

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

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

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;

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

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;
}
}

谁能看出问题出在哪里?

【问题讨论】:

  • 您确定将其包含在 java 构建路径中吗?
  • 你找到答案了吗?
  • 你在使用 proguard 吗?
  • This 可以帮忙

标签: java android error-handling jar


【解决方案1】:

添加这两个依赖

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      • 添加依赖:

        编译'org.apache.httpcomponents:httpcore:4.4.4'

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

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

          请注意,如果您只是将外部 JAR 文件添加到构建路径但忘记将存档也导入您的项目。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-09-28
            • 2023-03-22
            • 1970-01-01
            • 2017-09-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-23
            相关资源
            最近更新 更多