Image on Android alert on server using MultipartEntity

I am trying to upload image and data to a Django server. I included the apache-mime4j.0.6.jar and httpmime4.0.1.jar (Project-> build path-> Add external jar files) And here is the code for loading the 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 tried to manually create the libs folder and manually include the jar files in the / libs folder. When I do this, it does not compile.

Error:

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

I tried to create a new application, including libraries. And I came across the same error. I tried my best. Can someone tell me why this is happening and how to fix it. Any help would be greatly appreciated!

+6
source share
4 answers

If you are using the new android-sdk try this.

  • Create a folder named libs
  • Paste the jar file into this folder.
  • Finally, right-click your project> Build Path> Add External Archive.

What is it. This can help. Good luck.

+1
source

I upload an image from Android to a Django server using httpmime-4.2.1.jar . This is the only library I have included and it works great. Btw: libraries should be in the libs folder in Android projects.

this is the code i use to download.

 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(); } } 
0
source

I suggest you use spring-android. This is a good leisure apo library for android and spring-android, you can easily upload the file this way.

 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 the Apache http client on pre gingerbread and java.net.urlconnection on the gingerbread and higher level api, as Google suggests.

0
source

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

Click OK and everything will be fine.

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

0
source

All Articles