Question

I'm trying to get google pagerank on android with ADT with following code but it gives an NetworkOnMainThreadException on BufferedReader line.

String url = "http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&"
       + "ch=6" + hash + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + domain;
URLConnection conn = new URL(url).openConnection();

BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String input;
while ((input = br.readLine()) != null) {
    result = input.substring(input.lastIndexOf(":") + 1);
}

By the way I try this same code on PC java project and it works fine. I give following permission on AndroidManifest.xml too.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Était-ce utile?

La solution

This is a google your self question, in android you shouldn't let long duration work run in the ui thread ( this blocks ). avoid this exception by using async-task

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top