我正在开发Android应用,我想在youtube中搜索给定查询的第一个结果。
首先,我开始使用以下网址:
"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 :(得分:3)
试试这个
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
在应用此版本之前检查版本,请在活动开始时说明......