Hi all,
Recently, I want to develop a simple application to upload
videos to YouTube. But after reading the
tutorial of YouTube Data API.
(http://code.google.com/intl/zh-TW/apis/youtube/2.0/
developers_guide_protocol_direct_uploading.html)
I don't know how to fix the problem I encounter now.
The following is my code and after executing it, the HTTP
response code is 400 (Bad Request) and
the error message is "Incomplete Multipart Body".
Please give me some suggestions and directions.
Thank you very much.
[MyCode]
public void upload(String token, String clientID, String developerKey,
File file)
{
String youtubeUrl = "http://uploads.gdata.youtube.com/feeds/api/
users/default/uploads";
URL url = null;
try
{
url = new URL(youtubeUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setRequestProperty("Host",
"uploads.gdata.youtube.com");
conn.setRequestProperty("Authorization", "GoogleLogin auth=\"" +
token + "\"");
conn.setRequestProperty("GData-Version", "2");
conn.setRequestProperty("X-GData-Client", clientID);
conn.setRequestProperty("X-GData-Key", "key=" + developerKey);
conn.setRequestProperty("Slug", file.getName());
conn.setRequestProperty("Content-Type", "multipart/related;
boundary=\"" + boundary + "\"");
conn.setRequestProperty("Content-Length", getLength());
conn.setRequestProperty("Connection", "close");
DataOutputStream writer = new
DataOutputStream(conn.getOutputStream
());
writer.write(("\r\n--" + boundary + "\r\n").getBytes());
writer.write("Content-Type: application/atom+xml;
charset=UTF-8\r\n
\r\n".getBytes());
writer.write(getXml().getBytes("UTF-8"));
writer.write(("--" + boundary + "\r\n").getBytes());
writer.write("Content-Type: video/mp4\r\n".getBytes());
writer.write("Content-Transfer-Encoding:
binary\r\n\r\n".getBytes
());
FileInputStream reader = new FileInputStream(file);
byte[] array;
int bufferSize = Math.min(reader.available(), 2048);
array = new byte[bufferSize];
int read = 0;
read = reader.read(array, 0, bufferSize);
while ( read > 0)
{
writer.write(array, 0, bufferSize);
bufferSize = Math.min(reader.available(), 2048);
read = reader.read(array, 0, bufferSize);
}
writer.write(("--" + boundary + "--").getBytes());
writer.flush();
writer.close();
reader.close();
}
catch(Exception ex)
{
Log.e("Error", ex.toString());
}
}
private String getXml()
{
String xml = "<atom:entry
xmlns:atom=\"http://www.w3.org/2005/Atom\">
\r\n" +
"<media:group
xmlns:media=\"http://search.yahoo.com/mrss/\">\r\n"
+
"<media:category
scheme=\"http://gdata.youtube.com/schemas/2007/
categories.cat\">\r\n" +
"People\r\n" +
"</media:category>\r\n" +
"<media:category
scheme=\"http://gdata.youtube.com/schemas/2007/
developertags.cat\">alfa\r\n" +
"</media:category>\r\n" +
"<media:category
scheme=\"http://gdata.youtube.com/schemas/2007/
developertags.cat\"> 6ipt2uPhg9e46kl9 \r\n" +
"</media:category>\r\n" +
"<media:category
scheme=\"http://gdata.youtube.com/schemas/2007/
developertags.cat\">cF5GktQvk6f0kt8s\r\n" +
"</media:category>\r\n" +
"<media:description>Answers to Question
cF5GktQvk6f0kt8s\r\n" +
"</media:description>\r\n" +
"<media:keywords> alfa,
answer</media:keywords> <media:title> \r
\n" +
"Answers to Question
cF5GktQvk6f0kt8s\r\n" +
"</media:title>\r\n" +
"</media:group>\r\n" +
"</atom:entry>\r\n";
return xml;
}
private String getOutput()
{
StringBuilder sb = new StringBuilder();
sb.append("\r\n--" + boundary + "\r\n");
sb.append("Content-Type: application/atom+xml;
charset=UTF-8\r\n\r
\n");
try {
sb.append(new String(getXml().getBytes("UTF-8"),
"UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sb.append("--" + boundary + "\r\n");
sb.append("Content-Type: video/mp4\r\n");
sb.append("Content-Transfer-Encoding: binary\r\n\r\n");
return sb.toString();
}
private String getLength()
{
long length = getOutput().getBytes().length + ("--" + boundary +
"--").getBytes().length + file.length();
return String.valueOf(length);
}
[/MyCode]
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en