Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Created March 28, 2012 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Kieranties/2225346 to your computer and use it in GitHub Desktop.
Save Kieranties/2225346 to your computer and use it in GitHub Desktop.
Using HttpClient with MultipartEntity in Android
public static void executeMultipartPost(String url, String imgPath, String field1, String field2){
try {
HttpClient client = new DefaultHttpClient();
HttpPost poster = new HttpPost(url);
File image = new File(imgPath); //get the actual file from the device
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("field1", new StringBody(field1));
entity.addPart("field2", new StringBody(field2));
entity.addPart("image", new FileBody(image));
poster.setEntity(entity );
client.execute(poster, new ResponseHandler<Object>() {
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity respEntity = response.getEntity();
String responseString = EntityUtils.toString(respEntity);
// do something with the response string
return null;
}
});
} catch (Exception e){
//do something with the error
}
}
@agarwlGaurav
Copy link

Hi,

Can you share server-side code for receiving the file send as part of this Mutipart Post.

Thanks
Coding Crow
coding.crow@gmail.com

@mnazrin9
Copy link

Hi,

Can you share server-side code for receiving the file send as part of this Mutipart Post.
my email : mnazrin9@gmail.com

Thanks

Copy link

ghost commented Aug 29, 2014

What are the jar files we need to attach ?

@ranjankumar03
Copy link

Can you share server-side code for receiving the file send as part of this Mutipart Post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment