nasdneb
nasdneb

Reputation: 113

parse Multipart response in Android

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

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
    }

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!

Upvotes: 3

Views: 4942

Answers (2)

Martin Ždila
Martin Ždila

Reputation: 3219

It is also possible with apache-mime-4j:

HttpURLConnection conn = ...;
final InputStream is = conn.getInputStream();
try {
    final StringBuilder sb = new StringBuilder();
    sb.append("MIME-Version: ").append(conn.getHeaderField("MIME-Version")).append("\r\n");
    sb.append("Content-Type: ").append(conn.getHeaderField("Content-Type")).append("\r\n");
    sb.append("\r\n");

    parser.parse(new SequenceInputStream(new ByteArrayInputStream(sb.toString().getBytes("US-ASCII")), is));
} catch (final MimeException e) {
    e.printStackTrace();
} finally {
    is.close();
}

Upvotes: 0

nasdneb
nasdneb

Reputation: 113

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

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

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

But you can't use the native API, instead take this one http://code.google.com/p/javamail-android/

Now you can proceed handling your individual parts.

Upvotes: 5

Related Questions