所以我刚刚创建了一个解析器和一个处理程序来解析XML文件。它在我的Java测试项目中工作正常但在我的Android项目中没有。这是抱怨日志:
07-12 09:44:59.586:E / AndroidRuntime(1164):java.lang.RuntimeException:无法启动活动ComponentInfo {com.ec.ec/com.ec.ec.ListeOffresActivity}:android.os。 NetworkOnMainThreadException 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.os.Handler.dispatchMessage(Handler.java:99) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.os.Looper.loop(Looper.java:137) 07-12 09:44:59.586:E / AndroidRuntime(1164):在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):引起:android.os.NetworkOnMainThreadException 07-12 09:44:59.586:E / AndroidRuntime(1164):在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(InetAddress.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(HttpEngine.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):在android.app.Activity.performCreate(Activity.java:5008) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 07-12 09:44:59.586:E / AndroidRuntime(1164):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 07-12 09:44:59.586:E / AndroidRuntime(1164):... 11更多
看来我的模拟器没有连接到互联网,对吧?我该怎么办?
感谢您的帮助
答案 0 :(得分:4)
您正在主UI线程上运行Network request
。使用AsyncTask执行网络请求。
答案 1 :(得分:1)
日志不是很清晰。但我可以看到以下错误 -
07-12 09:44:59.586:E / AndroidRuntime(1164):at java.net.URL.openStream(URL.java:462)
因此,很可能URL无法从模拟器中加入,因为您可以从Java代码中访问和解析XMl。
您可以从模拟器启动浏览器,看看是否可访问网址甚至互联网?
-dattatray。