{"id":905,"date":"2013-09-27T23:37:42","date_gmt":"2013-09-27T15:37:42","guid":{"rendered":"http:\/\/www.vcoo.cc\/blog\/?p=905"},"modified":"2013-09-27T23:40:08","modified_gmt":"2013-09-27T15:40:08","slug":"android-network-onmain-thread-exception","status":"publish","type":"post","link":"https:\/\/vcoo.cc\/blog\/905\/","title":{"rendered":"Android\u5f02\u5e38NetworkOnMainThreadException\u5904\u7406"},"content":{"rendered":"<p>Android\u5f00\u53d1\u7684\u4e00\u5c0f\u9879\u76ee\uff0c\u8bbf\u95eeWebService\uff0c\u8bbf\u95ee\u7f51\u7edc\u65f6\u629b\u51fa\u8fd9\u4e2a\u5f02\u5e38\uff1aandroid.os.NetworkOnMainThreadException\u3002\u539f\u56e0\u662fAndroid\u4ece4.0\u5f00\u59cb\u4e0d\u80fd\u5728Honeycombe\u7684UI\u7ebf\u7a0b\u4e0a\u6267\u884c\u7f51\u7edcIO\uff0c\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u65e9\u671f\u7684android\u7248\u672c\u53ef\u4ee5\uff0c\u4f46\u662f\u8fd9\u4e2a\u771f\u7684\u4e0d\u662f\u4e00\u4e2a\u597d\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8ba9\u4f60\u7684app\u505c\u6b62\u54cd\u5e94\uff0c\u800c\u4e14\u53ef\u80fd\u4f1a\u5bfc\u81f4OS\u6740\u6b7b\u4f60\u7684app\u3002\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u4e00\u4e2a\u540e\u53f0\u8fdb\u7a0b\u6216\u8005\u662f\u7528AsyncTask\u6765\u6267\u884c\u4f60\u7684\u7f51\u7edc\u4e8b\u52a1\u3002<\/p>\n<p>\u5f02\u5e38\uff1a<strong>android.os.NetworkOnMainThreadException<\/strong><\/p>\n<p>\u5206\u6790\uff1a\u8fd9\u4e2a\u5f02\u5e38\u5927\u6982\u610f\u601d\u662f\u5728\u4e3b\u7ebf\u7a0b\u8bbf\u95ee\u7f51\u7edc\u65f6\u51fa\u7684\u5f02\u5e38\u3002 Android\u57284.0\u4e4b\u524d\u7684\u7248\u672c \u652f\u6301\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8bbf\u95ee\u7f51\u7edc\uff0c\u4f46\u662f\u57284.0\u4ee5\u540e\u5bf9\u8fd9\u90e8\u5206\u7a0b\u5e8f\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4e5f\u5c31\u662f\u8bf4\u8bbf\u95ee\u7f51\u7edc\u7684\u4ee3\u7801\u4e0d\u80fd\u5199\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4e86\u3002<\/p>\n<p><a href=\"http:\/\/www.vcoo.cc\/blog\/wp-content\/uploads\/2013\/09\/20130927230918.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-906\" alt=\"20130927230918\" src=\"http:\/\/www.vcoo.cc\/blog\/wp-content\/uploads\/2013\/09\/20130927230918-300x73.png\" width=\"300\" height=\"73\" srcset=\"https:\/\/vcoo.cc\/blog\/wp-content\/uploads\/2013\/09\/20130927230918-300x73.png 300w, https:\/\/vcoo.cc\/blog\/wp-content\/uploads\/2013\/09\/20130927230918-1024x250.png 1024w, https:\/\/vcoo.cc\/blog\/wp-content\/uploads\/2013\/09\/20130927230918.png 1324w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\u4e00\uff1a\uff082.3\u4ee5\u4e0b\u4e0d\u80fd\u7528\uff09<\/strong><\/p>\n<p>\u4ece Android 2.3 \u5f00\u59cb\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u7c7b StrictMode\uff0c\u8be5\u7c7b\u53ef\u4ee5\u7528\u4e8e\u6355\u6349\u53d1\u751f\u5728\u5e94\u7528\u7a0b\u5e8f\u4e3b\u7ebf\u7a0b\u4e2d\u8017\u65f6\u7684\u78c1\u76d8\u3001\u7f51\u7edc\u8bbf\u95ee\u6216\u51fd\u6570\u8c03\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6539\u8fdb\u7a0b\u5e8f\uff0c\u4f7f\u4e3b\u7ebf\u7a0b\u5904\u7406 UI \u548c\u52a8\u753b\u5728\u78c1\u76d8\u8bfb\u5199\u548c\u7f51\u7edc\u64cd\u4f5c\u65f6\u53d8\u5f97\u66f4\u5e73\u6ed1\uff0c\u907f\u514d\u4e3b\u7ebf\u7a0b\u88ab\u963b\u585e\u3002<\/p>\n<p>[java]<br \/>\n\/\/4.0\u4e0d\u80fd\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u8bbf\u95ee\u7f51\u7edc\u5904\u7406\uff0cStrictMode\u53ea\u57282.3\u4ee5\u4e0a\u7248\u672c\u624d\u6709<br \/>\n StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()<br \/>\n .detectDiskReads()<br \/>\n .detectDiskWrites()<br \/>\n .detectNetwork() \/\/ \u8fd9\u91cc\u53ef\u4ee5\u66ff\u6362\u4e3adetectAll() \u5c31\u5305\u62ec\u4e86\u78c1\u76d8\u8bfb\u5199\u548c\u7f51\u7edcI\/O<br \/>\n .penaltyLog() \/\/\u6253\u5370logcat\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u5b9a\u4f4d\u5230dropbox\uff0c\u901a\u8fc7\u6587\u4ef6\u4fdd\u5b58\u76f8\u5e94\u7684log<br \/>\n .build());<br \/>\n StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()<br \/>\n .detectLeakedSqlLiteObjects() \/\/\u63a2\u6d4bSQLite\u6570\u636e\u5e93\u64cd\u4f5c<br \/>\n .penaltyLog() \/\/\u6253\u5370logcat<br \/>\n .penaltyDeath()<br \/>\n .build());<\/p>\n<p>[\/java]<\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<p>[java]<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\n setContentView(R.layout.main);<br \/>\n \/\/ \u542f\u52a8\u7ebf\u7a0b\u6267\u884c\u4e0b\u8f7d\u4efb\u52a1<br \/>\n new Thread(downloadRun).start();<br \/>\n }<\/p>\n<p> \/**<br \/>\n * \u4e0b\u8f7d\u7ebf\u7a0b<br \/>\n *\/<br \/>\n Runnable downloadRun = new Runnable(){<\/p>\n<p>@Override<br \/>\n public void run() {<br \/>\n \/\/ TODO Auto-generated method stub<br \/>\n updateListView();<br \/>\n }<br \/>\n };<br \/>\n[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u5f00\u53d1\u7684\u4e00\u5c0f\u9879\u76ee\uff0c\u8bbf\u95ee\u7f51\u7edc\u65f6\u629b\u51fa\u8fd9\u4e2a\u5f02\u5e38\uff1aandroid.os.NetworkOnMainThreadException\u3002\u539f\u56e0\u662fAndroid\u4ece4.0\u5f00\u59cb\u4e0d\u80fd\u5728Honeycombe\u7684UI\u7ebf\u7a0b\u4e0a\u6267\u884c\u7f51\u7edcIO\uff0c\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u65e9\u671f\u7684android\u7248\u672c\u53ef\u4ee5\uff0c\u4f46\u662f\u8fd9\u4e2a\u771f\u7684\u4e0d\u662f\u4e00\u4e2a\u597d\u65b9\u6cd5\uff0c\u5b83\u4f1a\u8ba9\u4f60\u7684app\u505c\u6b62\u54cd\u5e94\uff0c\u800c\u4e14\u53ef\u80fd\u4f1a\u5bfc\u81f4OS\u6740\u6b7b\u4f60\u7684app\u3002\u4f60\u53ef\u80fd\u9700\u8981\u5728\u4e00\u4e2a\u540e\u53f0\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u4e00\u4e2a\u540e\u53f0\u8fdb\u7a0b\u6216\u8005\u662f\u7528AsyncTask\u6765\u6267\u884c\u4f60\u7684\u7f51\u7edc\u4e8b\u52a1\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[275,77,94,254,51],"class_list":["post-905","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-eclipse","tag-94","tag-254","tag-51","cat-76-id"],"_links":{"self":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/comments?post=905"}],"version-history":[{"count":0,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"wp:attachment":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}