10

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.

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?

Mat
  • 181
  • 1
  • 2
  • 13

5 Answers5

6

Add these two dependency

compile "org.apache.httpcomponents:httpcore:4.2.4"
compile "org.apache.httpcomponents:httpmime:4.3"
Sanjay Jain
  • 611
  • 8
  • 10
4

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.

Alexandros
  • 707
  • 2
  • 10
  • 23
1
  • Add dependency :

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

Download Apache HttpComponents jar file from this link http://hc.apache.org/downloads.cgi and 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.

Srinivasan Sekar
  • 1,799
  • 9
  • 21
0

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

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.

Community
  • 1
  • 1
Mick
  • 19,483
  • 1
  • 40
  • 91