2011-11-10 231 views
1

我有HTTP連接的問題,這是我的logcat:錯誤連接

11-10 11:31:22.036: E/AndroidRuntime(496): FATAL EXCEPTION: main 
11-10 11:31:22.036: E/AndroidRuntime(496): java.lang.RuntimeException: Unable to start activity ComponentInfo{fsi.sizeer.mcw/fsi.sizeer.mcw.McwActivity}: android.os.NetworkOnMainThreadException 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread.access$500(ActivityThread.java:122) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.os.Looper.loop(Looper.java:132) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread.main(ActivityThread.java:4123) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.lang.reflect.Method.invokeNative(Native Method) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.lang.reflect.Method.invoke(Method.java:491) 
11-10 11:31:22.036: E/AndroidRuntime(496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
11-10 11:31:22.036: E/AndroidRuntime(496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
11-10 11:31:22.036: E/AndroidRuntime(496): at dalvik.system.NativeStart.main(Native Method) 
11-10 11:31:22.036: E/AndroidRuntime(496): Caused by: android.os.NetworkOnMainThreadException 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.net.InetAddress.lookupHostByName(InetAddress.java:477) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.net.InetAddress.getAllByName(InetAddress.java:249) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038) 
11-10 11:31:22.036: E/AndroidRuntime(496): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523) 
11-10 11:31:22.036: E/AndroidRuntime(496): at java.net.URL.openStream(URL.java:645) 
11-10 11:31:22.036: E/AndroidRuntime(496): at fsi.sizeer.mcw.McwActivity.coversFileParse(McwActivity.java:79) 
11-10 11:31:22.036: E/AndroidRuntime(496): at fsi.sizeer.mcw.McwActivity.onCreate(McwActivity.java:71) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.Activity.performCreate(Activity.java:4397) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 
11-10 11:31:22.036: E/AndroidRuntime(496): ... 11 more 

這是我的方法:

public void coversFileParse(String URL) { 
    try { 
     URL url = new URL(URL); 
     DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); 
     Document document = dBuilder.parse(new InputSource(url.openStream())); 
     document.getDocumentElement().normalize(); 
     NodeList nodeListIssue = document.getElementsByTagName("Issue"); 

     for (int i = 0; i < nodeListIssue.getLength(); i++) { 
      Node node = nodeListIssue.item(i); 
      Element elementMain = (Element) node; 
      NamedNodeMap nodeListText = elementMain.getAttributes(); 
      Log.v(TAG, ""+nodeListText); 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我有上網權限和網絡連接,我必須重新啓動我的仿真器幾次和不想,你能幫助我嗎?

回答

2

下面兩行實際上是讓你知道錯誤是:

11-10 11:31:22.036: E/AndroidRuntime(496): Caused by: android.os.NetworkOnMainThreadException 
11-10 11:31:22.036: E/AndroidRuntime(496): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) 

好像你已經啓用StrictMode,導致它時,你使用的方式是錯誤的GUI線程(拋出一個異常即像上面那樣做網絡請求)。

的解決辦法是開始使用AsyncTask S,讓您的網絡流量被髮送,並在另一個「線程」處理:

public FileParseTask extends AsyncTask<Void, Void, Boolean> { 

    @Override 
    protected void onPreExecute() { 

     //Show dialogs here or similar 

    } 

    @Override 
    protected Boolean doInBackground(Void ... arg0) { 

     //Do your actual network operations here, as well as parsing 

    } 

    @Override 
    protected void onPostExecute(Boolean result) { 

     //Remove dialogs if any, and other "GUI"-updates that follow your parsing- 

    } 
} 

希望幫助你明白爲什麼會出現錯誤的understandning,和你將來如何防止它。

+0

THX,首先它應該是公共類的擴展。當我調用新的FileParseTask()。execute();它執行兩次,爲什麼? –

+0

嘿Dawid - 你確定你沒有循環'新的FileParseTask()。execute()'或類似的東西嗎? – ninetwozero