{"id":6746,"date":"2014-04-23T02:03:21","date_gmt":"2014-04-23T02:03:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/unable-to-start-receiver-android-os-networkonmainthreadexception-with-android-jellybean-collection-of-common-programming-errors\/"},"modified":"2014-04-23T02:03:21","modified_gmt":"2014-04-23T02:03:21","slug":"unable-to-start-receiver-android-os-networkonmainthreadexception-with-android-jellybean-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/23\/unable-to-start-receiver-android-os-networkonmainthreadexception-with-android-jellybean-collection-of-common-programming-errors\/","title":{"rendered":"Unable to start receiver: android.os.NetworkOnMainThreadException with android JellyBean-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;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.<\/p>\n<p>I saw here that i must move all internet connection to the main thread, right? At the moment i&#8217;ve a class called HttpRequest:<\/p>\n<pre><code>public class HttpRequest {    \n    private String url;\n\n    public HttpRequest(String url)\n    {       \n        this.url = url;\n    }\n\n    public String GetContent() throws ClientProtocolException, IOException\n    {\n        HttpClient client = new DefaultHttpClient();\n        HttpGet request = new HttpGet(url);\n        HttpResponse response = client.execute(request);\n\n        String html = \"\";\n        InputStream in = response.getEntity().getContent();\n        BufferedReader reader = new BufferedReader(new InputStreamReader(in));\n        StringBuilder str = new StringBuilder();\n        String line = null;\n        while((line = reader.readLine()) != null)\n        {\n            str.append(line);\n        }\n        in.close();\n        html = str.toString();\n        return html;\n    }\n}\n<\/code><\/pre>\n<p>Every minute is called updateAppWidget() that is a method of WidgetProvider class. Inside updateAppWidget() there is:<\/p>\n<pre><code>HttpRequest r = new HttpRequest(\"http:\/\/www.hwlogos.com\/test.json\");\nremoteViews.setTextViewText(R.id.ip, r.GetContent());\n<\/code><\/pre>\n<p>Can you tell me how solve it steep by steep? thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;ve a class called [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6746","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6746"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6746\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}