简体   繁体   中英

Android : can't load my XML file and app crashes

So I just created a parser and a handler to parse an XML file. It works fine in my Java test project but doesn't in my Android project. Here is the complaining log :

07-12 09:44:59.586: E/AndroidRuntime(1164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ec.ec/com.ec.ec.ListeOffresActivity}: android.os.NetworkOnMainThreadException 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.access$600(ActivityThread.java:130) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.Handler.dispatchMessage(Handler.java:99) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.Looper.loop(Looper.java:137) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.main(ActivityThread.java:4745) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.lang.reflect.Method.invokeNative(Native Method) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.lang.reflect.Method.invoke(Method.java:511) 07-12 09:44:59.586: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 07-12 09:44:59.586: E/AndroidRuntime(1164): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-12 09:44:59.586: E/AndroidRuntime(1164): at dalvik.system.NativeStart.main(Native Method) 07-12 09:44:59.586: E/AndroidRuntime(1164): Caused by: android.os.NetworkOnMainThreadException 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.InetAddress.getAllByName(InetAddr ess.java:214) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.(HttpConnection.java:70) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.(HttpConnection.java:50) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpEngine.sendRequest(HttpEngin e.java:239) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 07-12 09:44:59.586: E/AndroidRuntime(1164): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.URL.openStream(URL.java:462) 07-12 09:44:59.586: E/AndroidRuntime(1164): at com.ec.ec.ListeOffresActivity.onCreate(ListeOffresActivity.java:31) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.Activity.performCreate(Activity.java:5008) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 07-12 09:44:59.586: E/AndroidRuntime(1164): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 07-12 09:44:59.586: E/AndroidRuntime(1164): ... 11 more

It appears that my emulator isn't connected to the Internet, right ? What should I do ?

Thanks for your help

You are running your Network request on main UI thread. use AsyncTask to execute network request.

The log is not very cleay. But I can see the below error -

07-12 09:44:59.586: E/AndroidRuntime(1164): at java.net.URL.openStream(URL.java:462)

So most likely the URL is not accesisble from emulator, as you are able to access and parse the XMl from your java code.

Can you launch the browser from your emulator and see if the URL or even the internet is accessible?

-dattatray.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM