处理来自新泽西服务器Android客户端多部分响应 [英] Handling multipart response from Jersey server in Android client

查看:170
本文介绍了处理来自新泽西服务器Android客户端多部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功从Android发送多部分消息,这样的球衣服务器:

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));

这工作正常,但问题是,当我需要接收来自服务器GET多部分的响应。服务器还需要给我一个图像和三个字符串作为一个多部分消息。我不知道如何处理:

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));

我不知道如何从实体提取值。如何从响应得到的文件和字符串值?我知道如何处理像普通字符串或JSON简单回应,但这与多部分的反应令我烦恼。任何意见将是很有益的。谢谢你。

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.

推荐答案

有是消耗在客户端多部分的内容没有标准的方式,JAX-RS规范主要集中在物联网的服务器/资源结束。在一天结束时,虽然,用一个JAX-RS端点通信是pretty大致相同,与常规的HTTP服务器进行通信,并作为一种用于处理多部分HTTP响应将工作,例如任何现有的装置。在Java客户端的世界,它的普通pretty使用第三方库如 mime4j 处理多部分反应,但实际上孤单一个更简单的方式与新泽西做到这一点。该解决方案对JavaMail的API的依赖(通过 Maven的访问,除其他来源):

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());
}

一旦检索,您可以处理个人的身体部位以适合您的客户端code。

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

这篇关于处理来自新泽西服务器Android客户端多部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆