我的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();
}
}
我有互联网权限和网络连接,我已经重启了我的模拟器几次并且没有想,你能帮助我吗?
答案 0 :(得分: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
,以便在另一个“线程”上发送和处理您的网络流量:
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-
}
}
希望能帮助您了解错误发生的原因,以及如何在将来阻止错误。