Monday, January 25, 2010

HttpCommons and Ror

I finally got everything to work - in case this helps anyone else:

httpClient.getParams().setParameter("http.socket.timeout",
new Integer(90000)); // 90 second
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_0);
httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,
"UTF-8");

post = new HttpPost(new URI(url));

Charset chars = Charset.forName("UTF-8");
StringBody user_id = new StringBody("32432", chars);

MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartContent.addPart("media_entry[media]", new FileBody(file));
multipartContent.addPart("media_entry[user_id]", user_id);


post.setEntity(multipartContent);



HttpResponse response = httpClient.execute(post);
response.getEntity().getContent().close();


does the trick.

The important part is the forcing of the HTTP/1.0 protocol, since most rails web servers don't support chunked-encoding.

Saturday, January 23, 2010

Android and RoR Rest Services

In case this helps anyone else:
When you are doing multipart uploads from Android to Rails, you need to make sure that you tell the MultipartEntry to be Browser compatible:

MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartContent.addPart("media_entry[media]", new FileBody(file));
multipartContent.addPart("media_entry[user_id]", sb1);
and now I'll get back to our new project. More to come when we release an alpha version.