0

Possibly this is a duplicate question but I have already tried all answer of this site but none of this working!

I am using MultipartEntity for image upload. It's working completely fine in Emulator but When I check in device its not working.

Below my code

 HttpParams params = new BasicHttpParams();
 params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP);    

 HttpClient httpClient = new DefaultHttpClient(params);
 HttpContext localContext = new BasicHttpContext();
 HttpPost httpPost = new HttpPost("http://url.com/webservice_uploadvideo.php");

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);

 FileBody filebodyVideopre = new FileBody(new File(videoUploadpathPre)); //pre

 entity.addPart("fin_detail", filebodyVideopre);

 httpPost.setEntity(entity);

 HttpResponse response = httpClient.execute(httpPost, localContext);
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • why negative vote ?? – Nirav Ranpara Apr 21 '16 at 10:29
  • 1
    here is my blog explaining in detail how to make a multipart request in android :) http://learnwithmehere.blogspot.in/2015_10_01_archive.html have look at it read it carefully :) and here is my answer in SO http://stackoverflow.com/questions/35839130/send-file-and-parameters-to-server-with-httpurlconnection-in-android-api-23/35839455#35839455 for the similar question :) this is answerred based on my blog contents only :) have look at both of them :) bythe way I din down vote your question :D – Sandeep Bhandari Apr 21 '16 at 10:33
  • get help from this tutorial, i was also facing trouble using Multipart. "http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/" – Nilabja Apr 21 '16 at 10:34
  • ` MultipartEntity reqEntity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("UTF-8"));` u should modify like this – User Learning Apr 21 '16 at 10:36
  • Why are you using deprecated method still? – Piyush Apr 21 '16 at 10:40
  • @SandeepBhandari you could've mark the question duplicate. And btw, I up-voted the question. – Reaz Murshed Apr 21 '16 at 10:46
  • @reaz-murshed : Sorry am not pretty much experienced in SO :) I know that questions can be marked as duplicate but I never tried marking one by my self yet :) I'll keep in mind and will mark as duplicate next time onwards :) Thanks for notifying and thanks for the up-vote – Sandeep Bhandari Apr 21 '16 at 10:49

1 Answers1

1

Here's my working code for multi-part image uploading. Yes it works in real device.

private int uploadImage(String selectedImagePath) {

  try {
    HttpClient client = new DefaultHttpClient();
    File file = new File(selectedImagePath);
    HttpPost post = new HttpPost(Constants.URL);

    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                    Constants.BOUNDARY, Charset.defaultCharset());

    entity.addPart(Constants.MULTIPART_FORM_DATA_NAME, new FileBody(file));
    post.setHeader("Accept", "application/json");
    post.setHeader("Content-Type", "multipart/form-data; boundary=" + Constants.BOUNDARY);

    post.setEntity(entity);

    HttpResponse response = client.execute(post);
    HttpEntity httpEntity = response.getEntity();

    int status = response.getStatusLine().getStatusCode();

    return status;

  } catch (Exception e) {
    e.printStackTrace();
    return null;
  }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98