我已经设法将多部分消息从 Android 发送到 Jersey 服务器,如下所示:
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));
这很好,但问题是当我需要使用 GET 从服务器接收多部分响应时。服务器还需要向我发送一个图像和三个字符串作为多部分消息。我不确定如何处理:
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));
我不确定如何从实体中提取值。如何从响应中获取该文件和字符串值?我知道如何处理像普通字符串或 JSON 这样的简单响应,但是多部分响应让我很困扰。任何建议都会非常有帮助。谢谢你。