解析Android中的多部分响应

nas*_*neb 3 android mime-types

我正在使用 Multipart HttpPost 将图像和 json 文本从 android 客户端发送到 tomcat 服务器,反之亦然。将多部分实体发送到服务器没什么大不了的,因为您可以使用request.getPart(<name>). 但是在客户端,您只能以流的形式访问响应。所以我最终将 JSON 字符串和图像都附加到相同的后面,ServletOutputStream并且必须在客户端手动解析它们。我在网上找到了 apache-mime4j,但它几乎没有记录,我找不到一个如何使用它的例子。

在服务器端,我构建这样的响应:

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();
Run Code Online (Sandbox Code Playgroud)

在 Android 客户端,我想访问文本和图像数据:

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
    }
Run Code Online (Sandbox Code Playgroud)

在该方法中,body(BodyDescriptor bd, InputStream in)我的整个响应被视为text\plainmime 类型。所以我最终不得不再次手动解析每个字节,整个 apache-mime4j 都没用了。你能告诉我我做错了什么吗?谢谢!

nas*_*neb 5

好吧,我终于自己解决了。不,这是我所做的:

首先我需要multipart/mixed在服务器端创建一个响应。可以使用apache-mime-4jAPI来完成:

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();
Run Code Online (Sandbox Code Playgroud)

现在在客户端访问 HttpResponse 的 MIME 部分,我使用 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);
Run Code Online (Sandbox Code Playgroud)

但是您不能使用本机 API,而是使用这个http://code.google.com/p/javamail-android/

现在您可以继续处理您的各个零件。