2012-01-29 49 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); 

檢查版本,讓我們在活動開始時說...

相關問題