本文介绍了安卓的Youtube使用API​​ API级别11(或更高版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我想在YouTube搜索的第一个结果给定的查询。

I'm developing an Android app and I want to search for the first result in youtube to a given query.

首先,我开始使用以下网址:

Firstly, I started to use the following URL:

"http://gdata.youtube.com/feeds/api/videos?q="+ query + "&max-results=1"

它的工作原理很好的为Android API级别10(或更少),但它并不适用于API级别11(或更高版本)的工作。

It works good for Android API level 10 (or less) but it doesn't work for API level 11 (or higher).

于是,我试图使用的Youtube API 。我最初曾与SAXParser的错误,但它工作正常。再次,它工作正常,以API 10级(或更低),但对于API级别11(或更高版本),没有工作。

Then, I tried to use the Youtube API. I initially had errors with the SAXParser, but then it works fine.Once again, it works fine to API level 10 (or lower), but didn't work for API level 11 (or higher).

下面是我的code:

try{
    YouTubeQuery query = new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
    query.setOrderBy(YouTubeQuery.OrderBy.RELEVANCE);
    query.setFullTextQuery(pesquisa);
    query.setMaxResults(1);

    VideoFeed videoFeed = service.query(query, VideoFeed.class);

}
catch (Exception e) {
     Log.e("Exception", "exception", e);
}

这code抛出以下异常(API> = 11):

This code throws the following exception (api >= 11):

02-01 23:09:38.361: E/Exception(999): exception
02-01 23:09:38.361: E/Exception(999): android.os.NetworkOnMainThreadException
02-01 23:09:38.361: E/Exception(999):   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
02-01 23:09:38.361: E/Exception(999):   at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
02-01 23:09:38.361: E/Exception(999):   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-01 23:09:38.361: E/Exception(999):   at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-01 23:09:38.361: E/Exception(999):   at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:503)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.Service.getFeed(Service.java:1135)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.Service.getFeed(Service.java:1077)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:662)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.Service.query(Service.java:1237)
02-01 23:09:38.361: E/Exception(999):   at com.google.gdata.client.Service.query(Service.java:1178)
02-01 23:09:38.361: E/Exception(999):   at functions.Client.getYoutubeTrailer(Client.java:493)
02-01 23:09:38.361: E/Exception(999):   at com.moviemate.Movieprofile.getLinkYoutube(Movieprofile.java:673)
02-01 23:09:38.361: E/Exception(999):   at com.moviemate.Movieprofile.access$20(Movieprofile.java:655)
02-01 23:09:38.361: E/Exception(999):   at com.moviemate.Movieprofile$Loading$13.onClick(Movieprofile.java:625)
02-01 23:09:38.361: E/Exception(999):   at android.view.View.performClick(View.java:3460)
02-01 23:09:38.361: E/Exception(999):   at android.view.View$PerformClick.run(View.java:13955)
02-01 23:09:38.361: E/Exception(999):   at android.os.Handler.handleCallback(Handler.java:605)
02-01 23:09:38.361: E/Exception(999):   at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 23:09:38.361: E/Exception(999):   at android.os.Looper.loop(Looper.java:137)
02-01 23:09:38.361: E/Exception(999):   at android.app.ActivityThread.main(ActivityThread.java:4340)
02-01 23:09:38.361: E/Exception(999):   at java.lang.reflect.Method.invokeNative(Native Method)
02-01 23:09:38.361: E/Exception(999):   at java.lang.reflect.Method.invoke(Method.java:511)
02-01 23:09:38.361: E/Exception(999):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-01 23:09:38.361: E/Exception(999):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-01 23:09:38.361: E/Exception(999):   at dalvik.system.NativeStart.main(Native Method)

什么是做在YouTube的一个简单的查询的最佳方式?

What is the best way to do a simple query in youtube?

推荐答案

试试这个

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

检查版本在应用此之前,可以说,在活动的开始......

Check the version before you apply this, lets say at the beginning of the activity...

这篇关于安卓的Youtube使用API​​ API级别11(或更高版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:36