0

I want to upload a photo from the phone with some data like name and email.

From an Android device I know how to upload a photo and I know how to send data between the phone and the server but how do you do both at the same time?

Should I do them separately?

Kara
  • 6,115
  • 16
  • 50
  • 57
Ali Taqi
  • 5
  • 2

1 Answers1

0

In your case you should use the MultipartEntity class,

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("name", new StringBody(name));
            reqEntity.addPart("email", new StringBody(email));

            if(imagePath.trim().length() != 0) {
                reqEntity.addPart("profilePic", new FileBody(new File(imagePath))); 
            }

            HttpClient hc = new DefaultHttpClient();
            HttpPost postMethod = new HttpPost(urlString);
            HttpEntity resEntity;
            HttpResponse response = null;
            postMethod.setEntity(reqEntity);
            response = hc.execute(postMethod);
            resEntity = response.getEntity();
            response_str = EntityUtils.toString(resEntity);
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78