生活随笔
收集整理的這篇文章主要介紹了
Android之NetworkOnMainThreadException异常
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文鏈接:http://blog.csdn.net/mad1989/article/details/25964495 ??
點擊閱讀原文
------------------------------------------------------------------
看名字就應該知道,是網絡請求在MainThread中產生的異常
先來看一下官網的解釋:
Class Overview
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document?Designing for Responsiveness.
Also see?StrictMode.
http://developer.android.com/intl/zh-cn/reference/android/os/NetworkOnMainThreadException.html
解釋一下,從Honeycomb SDK(3.0)開始,google不再允許網絡請求(HTTP、Socket)等相關操作直接在Main Thread類中,其實本來就不應該這樣做,直接在UI線程進行網絡操作,會阻塞UI、用戶體驗相當bad!即便google不禁止,一般情況下我們也不會這么做吧~
所以,也就是說,在Honeycomb SDK(3.0)以下的版本,你還可以繼續在Main Thread里這樣做,在3.0以上,就不行了,建議
1,和network有關比較耗時的操作放到一個子線程里,然后用Handler消息機制與主線程通信。
[java] ?view plaincopy
public ? void ?onCreate(Bundle?savedInstanceState)?{?? ????super .onCreate(savedInstanceState);?? ????this .setContentView(R.layout.test);?? ?????? ????new ?Thread(networkTask).start();?? }?? ?? Handler?handler?=?new ?Handler()?{?? ????@Override ?? ????public ? void ?handleMessage(Message?msg)?{?? ????????super .handleMessage(msg);?? ????????Bundle?data?=?msg.getData();?? ????????String?val?=?data.getString("value" );?? ????????Log.i("mylog" ,? "請求結果為-->" ?+?val);?? ?????????? ?????????? ????}?? };?? ?? ? ? ?? Runnable?networkTask?=?new ?Runnable()?{?? ?? ????@Override ?? ????public ? void ?run()?{?? ?????????? ?????????? ????????Message?msg?=?new ?Message();?? ????????Bundle?data?=?new ?Bundle();?? ????????data.putString("value" ,? "請求結果" );?? ????????msg.setData(data);?? ????????handler.sendMessage(msg);?? ????}?? };??
2,使用異步機制如:asynctask,這個舉個簡單的加載網絡圖片的例子
[java] ?view plaincopy
class ?DownImage? extends ?AsyncTask?{?? ?? ????private ?ImageView?imageView;?? ?? ????public ?DownImage(ImageView?imageView)?{?? ????????this .imageView?=?imageView;?? ????}?? ?? ????@Override ?? ????protected ?Bitmap?doInBackground(String...?params)?{?? ????????String?url?=?params[0 ];?? ????????Bitmap?bitmap?=?null ;?? ????????try ?{?? ?????????????? ????????????InputStream?is?=?new ?URL(url).openStream();?? ????????????bitmap?=?BitmapFactory.decodeStream(is);?? ????????}?catch ?(Exception?e)?{?? ????????????e.printStackTrace();?? ????????}?? ????????return ?bitmap;?? ????}?? ?? ????@Override ?? ????protected ? void ?onPostExecute(Bitmap?result)?{?? ????????imageView.setImageBitmap(result);?? ????}?? }??
3,直接在main Thread 進行網絡操作的方法,網上給出的,我沒有具體測試:
在發起Http請求的Activity里面的onCreate函數里面添加如下代碼:
[java] ?view plaincopy
StrictMode.setThreadPolicy( new ?StrictMode.ThreadPolicy.Builder()?? ????????.detectDiskReads().detectDiskWrites().detectNetwork()?? ????????.penaltyLog().build());?? StrictMode.setVmPolicy(new ?StrictMode.VmPolicy.Builder()?? ????????.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()?? ????????.penaltyLog().penaltyDeath().build());??
請記住,如果在Main Thread里聲明了一個handler,這個handler所Post 的 Runnable(Thread)、以及處理的message都是在當前的mian線程里,非子線程。
總結
以上是生活随笔 為你收集整理的Android之NetworkOnMainThreadException异常 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。