I've managed to send multipart message from Android to Jersey server like this:
File file = new File(imagePath);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
FileBody fileContent = new FileBody(file);
MultipartEntity multipart = new MultipartEntity();
multipart.addPart("file", fileContent);
try {
multipart.addPart("string1", new StringBody(newProductObjectJSON));
multipart.addPart("string2", new StringBody(vitaminListJSON));
multipart.addPart("string3", new StringBody(mineralListJSON));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
httppost.setEntity(multipart);
HttpResponse response = null;
response = httpclient.execute(httppost);
String statusCode = String.valueOf(response.getStatusLine().getStatusCode());
Log.w("Status Code", statusCode);
HttpEntity resEntity = response.getEntity();
Log.w("Result", EntityUtils.toString(resEntity));
That's working fine but the problem is when I need to receive multipart response from server with GET. Server also needs to send me one image and three strings as a multipart message. I'm not sure how to handle that:
HttpResponse response = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity resEntity = response.getEntity();
Log.w("Result", EntityUtils.toString(resEntity));
I'm not sure how to extract values from entity. How to get that file and string values from response? I know how to handle simple response like normal String or JSON but this with multipart response bothers me. Any advice would be really helpful. Thank you.