YouTube上的视频上传 - Android

时间:2011-12-28 06:28:32

标签: android youtube youtube-api

我正致力于在Android上从YouTube上传视频的应用程序。我知道我们不能在Android上使用API​​,因此我们必须向YouTube发送http发布请求以上传视频。这是我的代码,我收到错误无效内容类型x-www-urlencode,代码显示我已使用Content-Type作为application / atom + xml; charset = UTF-8那么为什么我会遇到这种类型的错误,任何人都可以帮助我吗?

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");


List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
pairs.add(new BasicNameValuePair("Email", "xxxx"));
pairs.add(new BasicNameValuePair("Passwd", "xxx"));
pairs.add(new BasicNameValuePair("service", "youtube"));
pairs.add(new BasicNameValuePair("source", "YouTubeApp"));


post.setEntity(new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(post);

String textResponse = getResponseBody(response); 
String auth = textResponse.substring(textResponse.lastIndexOf("Auth"));

builder.setMessage(auth).show();

String entry = "<?xml version=\"1.0\"?>"
     + "<entry xmlns=\"http://www.w3.org/2005/Atom\""
     + " xmlns:media=\"http://search.yahoo.com/mrss/\""
     + " xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">"
     + "<media:group>"
     + "<media:title type=\"plain\">Bad Wedding Toast</media:title>"
     + "<media:description type=\"plain\">"
     + "I gave a bad toast at my friend's wedding. Maybe"
     + "</media:description>"
     + "<media:category"
     + " scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People"
     + "</media:category>"
     + "<media:keywords>toast, wedding</media:keywords>"
     + "</media:group>"
     + "</entry>";

StringBuilder sb = new StringBuilder();
sb.append("--");
 sb.append("Content-Type: application/atom+xml; charset=UTF-8"); 
sb.append(entry);
 sb.append("--");
 sb.append("Content-Type: video/3gpp");
sb.append("Content-Transfer-Encoding: binary");

               String bodyStart = sb.toString();

               File file = new File("/mnt/sdcard/recordvideooutput.3gpp");
               FileInputStream fIn = new FileInputStream(file);
               byte fileBytes[] = new byte[(int) file.length()];
               fIn.read(fileBytes);

            HttpClient client1 = new DefaultHttpClient();
            HttpPost post1 = new HttpPost("https://uploads.gdata.youtube.com/feeds/api/users/default/uploads");


            List<NameValuePair> pairs1 = new ArrayList<NameValuePair>();
            pairs1.add(new BasicNameValuePair("X-GData-Key", "key=xxxxx"));
            pairs1.add(new BasicNameValuePair("Slug", "/mnt/sdcard/recordvideooutput.3gpp"));
            pairs1.add(new BasicNameValuePair("Content-Type", "application/atom+xml; charset=UTF-8"));
            pairs1.add(new BasicNameValuePair("Content-Length", "" + (bodyStart.getBytes().length + fileBytes.length)));
            pairs1.add(new BasicNameValuePair("Authorization", "GoogleLogin " + auth));


post1.setEntity(new UrlEncodedFormEntity(pairs1));

response = client1.execute(post1);

1 个答案:

答案 0 :(得分:3)

您可以将API转换为Android应用程序的Google代码。此链接可能对您有用  Resources for Mobile Development with YouTube