找不到类'org.apache.http.entity.mime.content.Filebody',从方法中引用

Mat*_*Mat 9 java error-handling android jar

我要做的是将图片发送到Web服务器.当我在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;
Run Code Online (Sandbox Code Playgroud)

这就是该方法所在的类:

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

San*_*ain 5

添加这两个依赖

compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"
Run Code Online (Sandbox Code Playgroud)


Ale*_*ros 4

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