0

我的代码是:

    // get a new XmlPullParser object from Factory  

    XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
    xmlPullParserFactory.setNamespaceAware(false);
    XmlPullParser parser  = xmlPullParserFactory.newPullParser();

    // set input source  
    URL input = new URL(url);
    parser.setInput(input.openStream(), null);  

功能setInput失败。

你能帮我理解为什么吗?

当我使用 Android 2.2 时它可以正常工作,但在使用最新的 Android 4.2 时就不行了。这是相对的吗?

我再次检查它并失败,因为:input.openStream()

这是错误日志:

FATAL EXCEPTION: main
12-10 12:42:29.224: E/AndroidRuntime(1774): java.lang.IllegalStateException: Could not execute method of the activity
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.view.View$1.onClick(View.java:3597)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.view.View.performClick(View.java:4202)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.view.View$PerformClick.run(View.java:17340)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.os.Handler.handleCallback(Handler.java:725)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.os.Looper.loop(Looper.java:137)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at dalvik.system.NativeStart.main(Native Method)
12-10 12:42:29.224: E/AndroidRuntime(1774): Caused by: java.lang.reflect.InvocationTargetException
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.lang.reflect.Method.invokeNative(Native Method)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.lang.reflect.Method.invoke(Method.java:511)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.view.View$1.onClick(View.java:3592)
12-10 12:42:29.224: E/AndroidRuntime(1774):     ... 11 more
12-10 12:42:29.224: E/AndroidRuntime(1774): Caused by: android.os.NetworkOnMainThreadException
12-10 12:42:29.224: E/AndroidRuntime(1774):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at java.net.URL.openStream(URL.java:462)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at com.example.eduloops.LoginScreen.parse(LoginScreen.java:201)
12-10 12:42:29.224: E/AndroidRuntime(1774):     at com.example.eduloops.LoginScreen.onClickLoginButton(LoginScreen.java:130)
12-10 12:42:29.224: E/AndroidRuntime(1774):     ... 14 more
12-10 12:42:29.782: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property.
4

1 回答 1

0

您正在主线程上使用网络代码。在后台线程中进行联网。AsyncTask是一个不错的选择。

于 2012-12-10T15:41:59.667 回答