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();
}
}
我有上網權限和網絡連接,我必須重新啓動我的仿真器幾次和不想,你能幫助我嗎?
THX,首先它應該是公共類的擴展。當我調用新的FileParseTask()。execute();它執行兩次,爲什麼? –
嘿Dawid - 你確定你沒有循環'新的FileParseTask()。execute()'或類似的東西嗎? – ninetwozero