I'm having trouble on a certain piece of a current project and feel like I'm stuck right now. I'm trying to do a video upload with an HTTP post and multipart form data. I feel like I'm hit a wall in understanding the HTTP protocol and specifically multipart form data.
I have a URL to upload videos to in the format http://videoupload.thecompany.com/VideoApp.xml?method=upload&objectType=person&objectId=777777. I also need to include a title, description, and the videoFile of course. Are these the "multipart data"?
I've tried adapting this solution to meet my needs Upload video from Android to server?, and setting the extra data following all the other conn.setRequestProperty() calls like so:
conn.setRequestProperty("title", "video title");
conn.setRequestProperty("description", "video description");
But this isn't working for me. There's a comment from the original author of the code to add multipart form data about 30 lines later, but I don't understand why. Thanks for any help.
Here's the two step solution I came up with, largely from the information and links found here. This solution was easier for me to grasp than the upload2server() method in some of the related SO posts. Hope this helps someone else.
1) Select the video file from the gallery.
Create a variable
private static final int SELECT_VIDEO = 3;
-- it doesn't matter what number you use, so long as that's to one you check for later on. Then, use an intent to select a video.Use onActivityResult() to start the uploadVideo() method.
2) Go to http://hc.apache.org/downloads.cgi, download the latest HttpClient jar, add it to your project, and upload the video using the following method:
Once you have it working you'll probably want to put it in a thread and add an uploading dialog, but this will get you started. Working for me after I unsuccessfully tried the upload2Server() method. This will also work for images and audio with some minor tweaking.