2012-01-29 50 views
1

我正在开发一个Android应用程序,我想在给定的查询中搜索y​​outube中的第一个结果。Android:使用API​​ Level 11(或更高版本)的Youtube API

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

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

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

然后,我尝试使用Youtube API。我最初有SAXParser错误,但后来它工作正常。 再一次,它可以很好地适用于API级别10(或更低),但不适用于API级别11(或更高级别)。

这里是我的代码:

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

此代码抛出以下异常(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上简单查询的最佳方式?

+0

是什么,您有什么似乎是确切的问题,即在api 10中工作但不是11? – 2012-01-29 21:53:53

+0

当我使用API​​ 11时,它会在此行中引发异常: 'VideoFeed videoFeed = service.query(query,VideoFeed.class);' 当我使用api 10时,我可以获取视频。 – TGomews 2012-01-30 15:11:48

+0

你可以在你原来的帖子中发布异常吗? – 2012-01-30 19:28:59

回答

3

应用此之前,试试这个

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

检查版本,让我们在活动开始时说...

相关问题