解析Android的多部分响应 [英] parse Multipart response in Android

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

问题描述

我要送的图像和JSON文本从Android客户端到Tomcat服务器和其他方式通过使用多HttpPost的。发送多部分实体的服务器是没有什么大不了的,因为你可以很容易地使用 request.getPart处理部分(小于名称>)。但在客户端只能访问响应为流。所以,我结束了两个附加的JSON字符串和图像在同一 ServletOutputStream的键,必须手动解析他们在客户端。我发现Apache的mime4j在网络,但它几乎没有记载,我无法找到一个例子如何使用它。

I'm sending images and json text from the android client to a tomcat server and the other way around by using Multipart HttpPost's. Sending a Multipart Entity to the server is no big deal, because you can process the parts easily using request.getPart(<name>). But at the client side you can only access the response as a Stream. So I end up appending both, the JSON string and the image to the same ServletOutputStream and have to parse them by hand on the client side. I found apache-mime4j in the web but its hardly documented and I cant find a single example how to use it.

在服务器端我建立这样的反应:

On the server side I build the response like this:

ServletResponse httpResponse = ctx.getResponse();
ResponseFacade rf = (ResponseFacade) httpResponse;
rf.addHeader("Access-Control-Allow-Origin", "*");
rf.addHeader("Access-Control-Allow-Methods", "POST");
rf.addHeader("content-type", "multipart/form-data");
httpResponse.setCharacterEncoding("UTF-8");

MultipartResponse multi = new MultipartResponse((HttpServletResponse) httpResponse);
ServletOutputStream out = httpResponse.getOutputStream();

multi.startResponse("text/plain");
out.println(CMD + "#" + content);
multi.endResponse();

multi.startResponse("image/jpeg");
out.write(data);
multi.endResponse();

multi.finish();

ctx.complete();

而就在Android客户端我要访问的文本和图像数据:

And on the client side on Android I want to access the text and the image data:

InputStream is = response.getEntity().getContent();

MimeStreamParser parser = new MimeStreamParser();
MultipartContentHandler con = new MultipartContentHandler();
parser.setContentHandler(con);

try {
    parser.parse(is);
        String json = con.getJSON();        //get extracted json string
        byte[] imgBytes = con.getBytes();   //get extracted bytes

} catch (MimeException e) {
        e.printStackTrace();
} finally {
    is.close();
}

class MultipartContentHandler implements ContentHandler{

    public void body(BodyDescriptor bd, InputStream in) throws MimeException, IOException {
        //if MIME-Type is "text/plain"
        //   process json-part
        //else
        //   process image-part
    }

在方法体(BodyDescriptor万桶,为InputStream中)我的整个反应被视为文本\平原默类型。所以,我终于有手动重新解析每一个字节和整个Apache的mime4j是无用的。你能告诉我什么,我做错了什么?谢谢!

In the method body(BodyDescriptor bd, InputStream in) my whole response is treated as text\plain mime type. So I finally have to parse every byte manually again and the whole apache-mime4j is useless. Can you tell me what I am doing wrong? Thanks!

推荐答案

好吧,我终于解开了它自己。没有这里就是我所做的:

Ok i finally solved it myself. No here's what i did:

首先,我需要创建一个 /混合响应在服务器端。它可以使用的apache-MIME-4J API来完成:

First I need to create a multipart/mixed Response at the server side. It can be done using apache-mime-4j API:

ServletResponse httpResponse = ctx.getResponse();    
ResponseFacade rf = (ResponseFacade) httpResponse;
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("multipart/mixed");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "SEPERATOR_STRING",Charset.forName("UTF-8"));
entity.addPart("json", new StringBody(CMD + "#" + content, "text/plain",  Charset.forName("UTF-8")));
entity.addPart("image", new ByteArrayBody(data, "image/jpeg", "file"));

httpResponse.setContentLength((int) entity.getContentLength());

entity.writeTo(httpResponse.getOutputStream());
ctx.complete();

现在在客户端访问的Htt的presponse的MIME-部分我用的是javax.mail API。

Now at the client side to access the MIME-Parts of the HttpResponse I use the javax.mail API.

ByteArrayDataSource ds = new ByteArrayDataSource(response.getEntity().getContent(), "multipart/mixed");
MimeMultipart multipart = new MimeMultipart(ds);    
BodyPart jsonPart = multipart.getBodyPart(0);
BodyPart imagePart = multipart.getBodyPart(1);

但你不能使用原生API,而不是拿这个 HTTP: //$c$c.google.com/p/javamail-android/

现在,您可以继续处理您个人的部分。

Now you can proceed handling your individual parts.

这篇关于解析Android的多部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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