2012-11-23 81 views
-3

我試圖連接到Android中的URL地址以獲取HTML代碼。我正在關注tutorial。 readStream函數被調用時出現錯誤。這是日誌中的錯誤輸出:連接到Android應用程序中的網站並檢索html

11-23 20:24:22.572: W/WindowManager(151): Failure taking screenshot for (246x437) to layer 21010 
11-23 20:24:23.122: W/System.err(698): android.os.NetworkOnMainThreadException 
11-23 20:24:23.133: W/System.err(698): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 
11-23 20:24:23.133: W/System.err(698): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
11-23 20:24:23.133: W/System.err(698): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
11-23 20:24:23.133: W/System.err(698): at java.net.InetAddress.getAllByName(InetAddress.java:214) 
11-23 20:24:23.133: W/System.err(698): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 
11-23 20:24:23.142: W/System.err(698): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
11-23 20:24:23.142: W/System.err(698): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) 
11-23 20:24:23.142: W/System.err(698): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
11-23 20:24:23.153: W/System.err(698): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
11-23 20:24:23.153: W/System.err(698): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 
11-23 20:24:23.153: W/System.err(698): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) 
11-23 20:24:23.153: W/System.err(698): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) 
11-23 20:24:23.162: W/System.err(698): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 
11-23 20:24:23.162: W/System.err(698): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 
11-23 20:24:23.162: W/System.err(698): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 
11-23 20:24:23.162: W/System.err(698): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 
11-23 20:24:23.162: W/System.err(698): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271) 
11-23 20:24:23.172: W/System.err(698): at com.example.seminarska.Prevozi.onCreate(Prevozi.java:53) 
11-23 20:24:23.172: W/System.err(698): at android.app.Activity.performCreate(Activity.java:5008) 
11-23 20:24:23.172: W/System.err(698): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
11-23 20:24:23.172: W/System.err(698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
11-23 20:24:23.172: W/System.err(698): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
11-23 20:24:23.182: W/System.err(698): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
11-23 20:24:23.182: W/System.err(698): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
11-23 20:24:23.182: W/System.err(698): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-23 20:24:23.194: W/System.err(698): at android.os.Looper.loop(Looper.java:137) 
11-23 20:24:23.194: W/System.err(698): at android.app.ActivityThread.main(ActivityThread.java:4745) 
11-23 20:24:23.203: W/System.err(698): at java.lang.reflect.Method.invokeNative(Native Method) 
11-23 20:24:23.212: W/System.err(698): at java.lang.reflect.Method.invoke(Method.java:511) 
11-23 20:24:23.212: W/System.err(698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
11-23 20:24:23.212: W/System.err(698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-23 20:24:23.223: W/System.err(698): at dalvik.system.NativeStart.main(Native Method) 

下面的代碼是我正在使用的代碼。

try { 
    URL url = new URL("http://www.vogella.com"); 
    HttpURLConnection con = (HttpURLConnection) url 
    .openConnection(); 
    readStream(con.getInputStream()); 
    } catch (Exception e) { 
    e.printStackTrace(); 
} 



private void readStream(InputStream in) { 
    BufferedReader reader = null; 
    try { 
    reader = new BufferedReader(new InputStreamReader(in)); 
    String line = ""; 
    while ((line = reader.readLine()) != null) { 
     System.out.println(line); 
    } 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    if (reader != null) { 
     try { 
     reader.close(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 
    } 
} 

任何想法是什麼錯?有沒有不同的方式來連接到網絡。

回答

2

由於您正在執行網絡操作,例如在主用戶界面線程中檢索HTML,您正在收到此錯誤。

要解決這個問題的最好方式,將所有與網絡相關的代碼放到一個單獨的ThreadAsyncTask

+0

你知道任何很好的例子或教程來做到這一點? –

+0

Vogella本身有一些關於後臺處理的很好的教程,它將涵蓋這一點。你可以試試這些。 –

相關問題