我正在使用Google API Java客户端库(http://code.google.com/p/google-api-java-client/)将我的Android手机上的视频上传到YouTube。
这是我到目前为止的代码:
// Create the meta data
Entry entry = new Entry();
entry.group = new MediaGroup();
entry.group.title = "test title";
entry.group.description = "test description";
entry.group.category = new MediaCategory();
entry.group.category.Cat = "People\n";
XmlNamespaceDictionary namespaceDictionary = Util.NAMESPACE_DICTIONARY;
AtomContent aContent = new AtomContent();
aContent.entry = entry;
aContent.namespaceDictionary = namespaceDictionary;
// Create the actual video data
InputStreamContent bContent = new InputStreamContent();
bContent.inputStream =
context.getContentResolver().openInputStream(media);
bContent.type = context.getContentResolver().getType(media);
bContent.length = videoAsset.getLength();
// Build up the POST request
MultipartRelatedContent multiContent = new MultipartRelatedContent();
multiContent.parts.add(aContent);
multiContent.parts.add(bContent);
HttpRequest request = transport.buildPostRequest();
request.url = YouTubeUrl.uploadUrl();
GoogleHeaders headers = (GoogleHeaders) request.headers;
headers.setSlugFromFileName("mytest.3gpp");
request.content = multiContent;
request.headers.contentType = multiContent.getType();
request.execute();
不幸的是,我在调用request.execute()时遇到异常:
03-06 23:28:58.887: WARN/System.err(18923): com.google.api.client.http.HttpResponseException: 415 Unsupported Media Type
03-06 23:28:58.897: WARN/System.err(18923): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:221)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.YouTubeAPI.upload(YouTubeAPI.java:118)
03-06 23:28:58.897: WARN/System.err(18923): at com.abless.vidshare.VidShare.onActivityResult(VidShare.java:185)
03-06 23:28:58.897: WARN/System.err(18923): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 23:28:58.907: WARN/System.err(18923): at android.os.Looper.loop(Looper.java:144)
03-06 23:28:58.907: WARN/System.err(18923): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-06 23:28:58.907: WARN/System.err(18923): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 23:28:58.917: WARN/System.err(18923): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 23:28:58.917: WARN/System.err(18923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 23:28:58.917: WARN/System.err(18923): at dalvik.system.NativeStart.main(Native Method)
我做错了什么?看起来像MultipartRelatedContent没有自动指定Content-Length,这可能是问题吗?我查看了实现代码,MultipartRelatedContent.getLength()返回-1,这不是很有帮助。
是否有人设法使用Google API Java客户端库将视频成功上传到YouTube?
感谢。
- 亚历山大
编辑:即使我填写了Content-Length部分,我仍然会得到相同的错误(415 Unsupported Media Type),因此无法解决。几乎看起来POST标题是错误的?
答案 0 :(得分:0)
自己搞清楚......结果显示了YouTubeURL使用了JsonC选项。在评论出这条线之后,一切都很美妙。
答案 1 :(得分:0)
如果有人对使用新google-api-java-client的示例感兴趣,我已将其附加到bug 16。