Android post image to the Server using MultipartEntity
Asked Answered
B

4

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!!

Bodywork answered 2/11, 2012 at 5:49 Comment(0)
W
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.

Workshop answered 2/11, 2012 at 6:16 Comment(2)
Yes I did the same. I have mentioned itBodywork
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. :)Workshop
Z
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();
    }
}
Zip answered 2/11, 2012 at 6:16 Comment(2)
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.Zip
could you give a usage example please..? Would be very helpfulHerv
C
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.

Chema answered 2/11, 2012 at 6:59 Comment(2)
what is RestUtil? It cannot be resolved even after I imported 9 downloaded packages.. and what is YourResultDTO?Herv
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.Chema
A
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)

Antiproton answered 13/5, 2014 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.