次のように、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 を使用してサーバーからマルチパート応答を受信する必要がある場合です。サーバーは、1 つの画像と 3 つの文字列をマルチパート メッセージとして送信する必要もあります。それを処理する方法がわかりません:
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 などの単純な応答を処理する方法は知っていますが、これはマルチパート応答では気になります。どんなアドバイスも本当に役に立ちます。ありがとうございました。