视频上传YouTube - Android



我正在开发从android上传视频到YouTube的应用程序。我知道我们不能在android上使用API,所以我们必须发送一个http post请求到YouTube上传视频。这是我的代码,我得到错误无效的内容类型x-www-urlencode,代码显示我已经使用内容类型作为应用程序/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);

您可以将API转换为android应用程序的google代码。这个链接可能对你有用基于YouTube的手机开发资源

相关内容

  • 没有找到相关文章

最新更新