Handling multipart response from Jersey server in

2019-05-31 05:38发布

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.

1条回答
相关推荐>>
2楼-- · 2019-05-31 05:54

There is no standard way to consume multipart content on the client side, the JAX-RS specification focuses mainly on the server/resource end of things. At the end of the day though, communicating with a JAX-RS endpoint is pretty much the same as communicating with a regular HTTP server, and as such any existing means for processing multipart HTTP responses will work. In the Java client world, its pretty common to use a third party library like mime4j to process multipart responses, but theres actually an easier way to do this with Jersey. The solution has a dependency on the JavaMail API (accessible via Maven, amongst other sources):

final ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
        Boolean.TRUE);
final Client client = Client.create(config);

final WebResource resource = client
        .resource(URL_HERE);
final MimeMultipart response = resource.get(MimeMultipart.class);

// This will iterate the individual parts of the multipart response
for (int i = 0; i < response.getCount(); i++) {
    final BodyPart part = response.getBodyPart(i);
    System.out.printf(
            "Embedded Body Part [Mime Type: %s, Length: %s]\n",
            part.getContentType(), part.getSize());
}

Once retrieved, you can process the individual body parts as appropriate for your client code.

查看更多
登录 后发表回答