6

I have been trying to upload an image and data to Django server. I have included apache-mime4j.0.6.jar and httpmime4.0.1.jar libraries ( Project->build path->Add external jar files) And here's the code to upload an image.

HttpResponse response = null;
try {
    HttpPost httppost = new HttpPost("http://10.0.2.2:8000/mobile");
    //  HttpPost httppost = new HttpPost("some url");

    MultipartEntity multipartEntity = new MultipartEntity(); //MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    multipartEntity.addPart("name", new StringBody("nameText"));
    multipartEntity.addPart("place", new StringBody("placeText"));
    multipartEntity.addPart("tag", new StringBody("tagText"));
    //multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
    multipartEntity.addPart("Image", new FileBody(destination));
    httppost.setEntity(multipartEntity);

    httpclient.execute(httppost, new PhotoUploadResponseHandler());

  } catch (Exception e) {
    Log.e( "Error","error");
  } 

Error message:

Could not find class 'org.apache.http.entity.mime.MultipartEntity'

And I have tried manually creating libs folder and manually including jar files into /libs folder. When I do that It fails to compile.

Error:

Conversion to Dalvik format failed with error 1  Unknown Android Packaging Problem

Tried creating fresh application including libraries. And I encountered the same error. I've tried everything possible. Can anyone tell me why this happens and how to fix it. Any help would be greatly appreciated!!

Unni Kris
  • 3,081
  • 4
  • 35
  • 57
Geetanjali
  • 101
  • 1
  • 1
  • 5

4 Answers4

1

If you are using new android-sdk Try this.

  1. Create folder named as libs
  2. Paste your jar file in that folder.
  3. And finally right click on your project > Build Path > Add External Archive.

That's it.This might help.Good Luck.

Akshay
  • 2,506
  • 4
  • 34
  • 55
  • Yes I did the same. I have mentioned it – Geetanjali Nov 02 '12 at 07:06
  • Have you followed the steps I mention because then it should not give you class not found error.If you just added the library by using the way you did will not work.Because this is the changes in new sdk.What you did is correct with previous sdk but with new it will not work. :) – Akshay Nov 02 '12 at 07:14
0

I upload an image from Android to a Django server using httpmime-4.2.1.jar. That is the only library that i have included and it works fine. Btw: libraries are supposed to be in the libs folder in Android projects.

this is the code that i am using for the upload.

private JSONObject uploadImage(String url, File img) throws Exception{
    url = addAuthToken(url);
    HttpPost post = new HttpPost(url);
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    FileBody fileBody = new FileBody(img, "images/jpeg");
    reqEntity.addPart("image", fileBody);
    post.setEntity(reqEntity);

    JSONObject ret = baseRequest(post);
    checkReturnStatus(ret, 201);

    return ret;
}

private JSONObject baseRequest(HttpUriRequest request) throws Exception{
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);
    BufferedReader in = null;
    try{
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer();
        String line = null;
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }

        return new JSONObject(sb.toString());
    }finally {
        if(in != null) in.close();
    }
}
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Is the jar library in the libs folder? This is necessary for it to be packed correctly into the APK. You can remove and add it one more time. Drag it from a file explorer directly into the folder in eclipse. You should then see it under "Android Dependencies", where you can open it and see if the required class is in it. – SimonSays Nov 02 '12 at 16:32
  • could you give a usage example please..? Would be very helpful – Droidman Dec 14 '12 at 13:03
0

I suggest you to use spring-android It is a good rest api library for android and with spring-android, you can upload file easily like this.

HttpHeaders requestHeaders = ....
MultiValueMap<String, Object> message = new LinkedMultiValueMap<String, Object>();
message.add("qqfile", new FileSystemResource(filePath)); //attach file

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
            message, requestHeaders);
 RestTemplate restTemplate = RestUtil.getRestTemplate( context);
 ResponseEntity<YourResultDTO> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,YourResultDTO.class);

It is very easy to use and use Apache http client on pre gingerbread and java.net.urlconnection on gingerbread and higher api level as google suggests.

kingori
  • 2,406
  • 27
  • 30
  • what is RestUtil? It cannot be resolved even after I imported 9 downloaded packages.. and what is YourResultDTO? – Droidman Dec 14 '12 at 15:17
  • You should replace YourResultDTO to your own DTO class. It will be the result object that holds result from server and if you set spring-android properly, spring-android would handle parsing json / xml result. – kingori Dec 15 '12 at 06:15
0

I had the same problem and I solved it this way: in the java build path window, click the Order and Export tab Highlight the library you want to add then click the up (upload lib to be first)

Click ok and everything is fine.

Here is a link with photos (Android Eclipse NoClassDefFoundError for external .jar files)

Community
  • 1
  • 1