Reputation: 2095
I'm dealing with the following problem: when trying to upload an image through MultiPart Entity and it seems that the several updates on HttpClient/MIME are cracking everything. I'm trying the following code, but it fails with a NoClassDefFoundError
:
public static void executeMultipartPost(File image, ArrayList<Cookie> cookies, String myUrlToPost) {
try {
// my post instance
HttpPost httppost = new HttpPost(myUrlToPost);
// setting cookies for the connection session
if (cookies != null && cookies.size() > 0) {
String cookieString = "";
for (int i=0; i<cookies.size(); ++i) {
cookieString += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}
cookieString += "domain=" + BaseUrl + "; " + "path=/";
httppost.addHeader("Cookie", cookieString);
}
// creating the http client
HttpClient httpclient = new DefaultHttpClient();
// creating the multientity part [ERROR OCCURS IN THIS BELLOW LINE]
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("photoupload", new FileBody(image));
httppost.setEntity(multipartEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
} catch (Exception e) {}
}
This method is fully compilable and uses the httpclient-4.0.1.jar and httpmime-4.2.jar libs, but again, it crashes on the commented line for me.
Am I missing something?
Upvotes: 1
Views: 1469
Reputation: 19102
Ok, here you have libraries needed for Multipart post, all credits to Satya Komatineni and David Maclean, writers of Pro Android 3 and now 4 and I quote from their book
To do multipart POST calls, you need to get three additional Apache open source projects: Apache Commons IO, Mime4j, and HttpMime. You can download these projects from the following web sites: Commons IO: http://commons.apache.org/io/ Mime4j: http://james.apache.org/mime4j/ HttpMime: http://hc.apache.org/downloads.cgi (inside of HttpClient)
Upvotes: 4
Reputation: 1667
I have the same problem and it was fixed by moving jars files into 'libs' directory in your android project.
Upvotes: 1
Reputation: 9910
Make sure the jar is checked in the projects build path. (Project -> Properties)
Upvotes: 0