【问题标题】:Handling multipart response from Jersey server in Android client在 Android 客户端中处理来自 Jersey 服务器的多部分响应
【发布时间】:2013-02-11 06:26:18
【问题描述】:

我已经成功地将多部分消息从 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 这样的简单响应,但是多部分响应让我很困扰。任何建议都会非常有帮助。谢谢。

【问题讨论】:

    标签: java android rest jersey multipart


    【解决方案1】:

    没有在客户端消费多部分内容的标准方法,JAX-RS 规范主要关注事物的服务器/资源端。归根结底,与 JAX-RS 端点通信与与常规 HTTP 服务器通信几乎相同,因此任何现有的处理多部分 HTTP 响应的方法都可以工作。在 Java 客户端世界中,使用像 mime4j 这样的第三方库来处理多部分响应是很常见的,但实际上使用 Jersey 有一种更简单的方法。该解决方案依赖于 JavaMail API(可通过Maven 访问,以及其他来源):

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

    一旦检索到,您就可以根据您的客户端代码处理各个正文部分。

    【讨论】:

    • 感谢您的回复。最后,我通过发送一个包含所有数据的大型 JSON 来处理这种情况。我什至将图像转换为 byte[] :)
    • 没问题。如果您将来需要处理多部分,答案就在那里!
    猜你喜欢
    • 2014-01-09
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多