tags:

views:

86

answers:

1

I've been trying to find a way to upload a video from an Android device to an API, but I haven't found a good way to do it. It seems most of the information I've found online is fairly out of date (a lot of it being from last year). Most of them are using a method like this: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html

What's the easiest/preferred way to upload something to an API with a multipart POST?

A: 

I have an Android app I'm developing against the Campfire chat service's "API". The code here uploads a file through multipart POST:

http://github.com/klondike/android-campfire/blob/master/src/com/github/klondike/java/campfire/Room.java#L175

Everything after the "dos.close()" line is related to checking the response to detect whether the post was successful.

Not everything in there is necessary for every multi-part post; for example, the X-Requested-With header is specific to Campfire, the User-Agent is optional, and the Cookie is because I have to stay logged in. Also, the "OH MY GOD" comment about spacing is probably Campfire-specific.

I've heard that the latest version of the HttpClient library from Apache has more convenient built-in multi-part support, but the last sync Google performed against it to Android didn't include those features, so here I am doing it manually.

Hope that's of some help.

Klondike