Reputation: 3084
I have problem with http connection, this is my 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
This is my method:
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();
}
}
I have Internet permission and Network connection, I have restart my emulator a few times and nothink, can You help me ?
Upvotes: 1
Views: 1078
Reputation: 6071
The following two lines actually let you know what the error is:
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)
It seems like you have enabled StrictMode
, causing it to throw an Exception whenever you utilize the GUI-thread in the wrong way (ie do network requests like you're doing above).
The solution would be to start using AsyncTask
s, so that your network traffic gets sent and processed on another "thread":
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-
}
}
Hope that helps you get an understandning of why the error occurs, and how you can prevent it in the future.
Upvotes: 2