Handling multipart response from Jersey server in Android client

人走茶凉 提交于 2019-12-08 05:47:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!