TGomews
TGomews

Reputation: 68

Android: Youtube API using API Level 11 (or higher)

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"

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

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).

Here is my 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);                    
}

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)

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

Upvotes: 1

Views: 1189

Answers (1)

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

Try this

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...

Upvotes: 3

Related Questions