2011-12-28 122 views
1

我正在从android上上传YouTube上的视频。我知道我们不能在android上使用API​​,所以我们必须发送一个http post请求到YouTube上传视频。这里是我的代码,我得到错误无效的Content-Type x-www-urlencode,代码显示我已经使用Content-Type作为application/atom + xml; charset = UTF-8那么为什么我得到这种类型的错误,任何人都可以帮助我呢?YouTube上的视频上传 - Android

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); 

回答