android.os.NetworkOnMainThreadException如何解決?

error以下:java

05-06 11:38:43.104: D/(23666): android.os.NetworkOnMainThreadExceptionandroid

05-06 11:39:07.800: D/GestureDetector(23666): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 5 mFalseSizeCnt:0api

05-06 11:39:07.800: I/QCNEA(23666): |NIMS| getaddrinfo: hostname www.baidu.com servname NULL numeric 4 appname app

05-06 11:39:07.800: W/System.err(23666): android.os.NetworkOnMainThreadExceptionide

05-06 11:39:07.810: W/System.err(23666): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128)oop

05-06 11:39:07.810: W/System.err(23666): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)ui

05-06 11:39:07.810: W/System.err(23666): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)this

05-06 11:39:07.810: W/System.err(23666): at java.net.InetAddress.getAllByName(InetAddress.java:214)url

05-06 11:39:07.810: W/System.err(23666): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)spa

05-06 11:39:07.810: W/System.err(23666): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)

05-06 11:39:07.810: W/System.err(23666): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)

05-06 11:39:07.810: W/System.err(23666): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)

05-06 11:39:07.810: W/System.err(23666): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)

05-06 11:39:07.820: W/System.err(23666): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)

05-06 11:39:07.820: W/System.err(23666): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)

05-06 11:39:07.820: W/System.err(23666): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)

05-06 11:39:07.830: W/System.err(23666): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)

05-06 11:39:07.830: W/System.err(23666): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)

05-06 11:39:07.830: W/System.err(23666): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)

05-06 11:39:07.830: W/System.err(23666): at com.example.networkconnectiondemo.MainActivity.onClick(MainActivity.java:76)

05-06 11:39:07.840: W/System.err(23666): at android.view.View.performClick(View.java:4367)

05-06 11:39:07.840: W/System.err(23666): at android.view.View$PerformClick.run(View.java:17984)

05-06 11:39:07.840: W/System.err(23666): at android.os.Handler.handleCallback(Handler.java:725)

05-06 11:39:07.840: W/System.err(23666): at android.os.Handler.dispatchMessage(Handler.java:92)

05-06 11:39:07.840: W/System.err(23666): at android.os.Looper.loop(Looper.java:137)

05-06 11:39:07.840: W/System.err(23666): at android.app.ActivityThread.main(ActivityThread.java:5297)

05-06 11:39:07.840: W/System.err(23666): at java.lang.reflect.Method.invokeNative(Native Method)

05-06 11:39:07.840: W/System.err(23666): at java.lang.reflect.Method.invoke(Method.java:511)

05-06 11:39:07.840: W/System.err(23666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)

05-06 11:39:07.840: W/System.err(23666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)

05-06 11:39:07.840: W/System.err(23666): at dalvik.system.NativeStart.main(Native Method)

個人代碼以下:

@Override

public void onClick(View v)

{

switch (v.getId())

{

case R.id.activity_main_btn:

try

{

// Toast.makeText(this, "I'm here!", Toast.LENGTH_LONG).show();

/* 獲取url並獲取數據 */

// 清空顯示內容的文本框

tvShowContent.setText("");

// 獲取用戶輸入的url

URL url = new URL(etInputUrl.getText().toString());

//URL url = new URL("http://www.baidu.com");

// 聲明打開連接

URLConnection urlConnection = url.openConnection();

// 獲取網頁數據

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(urlConnection.getInputStream()));

// 逐行讀取時的字符串

String line = "";

// 讀取數據

while ((line = bufferedReader.readLine()) != null)

{

Message message = new Message();

message.obj = line;

message.what = 0;

// 將數據顯示在界面上

handler.sendMessage(message);

}


} catch (Exception e)

{

e.printStackTrace();

Log.d("", e.toString());

}

break;

。。。。。。。。。

經過debug能夠發如今執行

// 獲取網頁數據

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(urlConnection.getInputStream()));

這段代碼時報的錯。

那麼如何修改呢?

根據api文檔介紹,只要在調用這段代碼以前加上以下代碼便可:

StrictMode

.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()

.detectDiskReads().detectDiskWrites()

.detectNetwork() // or .detectAll() for all

// detectable problems

.penaltyLog().build());

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()

.detectLeakedSqlLiteObjects()

.detectLeakedClosableObjects().penaltyLog()

.penaltyDeath().build());

相關文章
相關標籤/搜索