Unable to start receiver: android.os.NetworkOnMainThreadException with android JellyBean-Collection of common programming errors
I’m trying to develop a widget that updates itself every minute by a json resource, my problem is that when i try to launch it on jellybean it crashes, while with gingerbread works.
I saw here that i must move all internet connection to the main thread, right? At the moment i’ve a class called HttpRequest:
public class HttpRequest {
private String url;
public HttpRequest(String url)
{
this.url = url;
}
public String GetContent() throws ClientProtocolException, IOException
{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
return html;
}
}
Every minute is called updateAppWidget() that is a method of WidgetProvider class. Inside updateAppWidget() there is:
HttpRequest r = new HttpRequest("http://www.hwlogos.com/test.json");
remoteViews.setTextViewText(R.id.ip, r.GetContent());
Can you tell me how solve it steep by steep? thanks