I'm sending a message built with google's protocol buffer from and android device using this code:
// Set up the HttpClient
HttpClient client = new DefaultHttpClient();
String url = "http://192.168.2.103:8888/sdroidmarshal";
HttpPost postRequest = new HttpPost(url);
// Create the content for the message
AbstractContentBody[] parts = new AbstractContentBody[1];
InputStream ins = new ByteArrayInputStream(offers.build().toByteArray());
parts[0] = new InputStreamBody(ins, "sdroidmsg");
// Add the content to the message
MultipartEntity requestContent = new MultipartEntity();
requestContent.addPart("message", parts[0]);
// Send!
postRequest.setEntity(requestContent);
client.execute(postRequest);
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(postRequest, responseHandler);
} catch (Throwable t) {
}
Eventually this code will actually be sending more than one part...
I have a servlet running on Google's app engine that receives this post request and at the moment only has the following code:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doPost(req, resp);
try {
ServletFileUpload upload = new ServletFileUpload();
resp.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
log.warning("Got a form field: " + item.getFieldName());
} else {
log.warning("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}
Obviously the server doesn't do very much right now! But I have noticed that it seems to receive two parts both called "message" and file "sdroidmsg", which I really don't understand. Surely it should only receive this once? I thought perhaps that sdroidmsg might be being split into two part because of size, but that is a complete guess, I don't really know the inner working of what's happening behind the scenes. Could anyway explain why this is happening? I can post more code if required.