public URL setUrl() throws Exception{
System.out.println("1");
URL iotd=new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
System.out.println("2");
BufferedReader in=new BufferedReader(new InputStreamReader(iotd.openStream()));
System.out.println("3");**//this never gets printed**
//testing xml parser
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
System.out.println("4");
XmlPullParser xpp=factory.newPullParser();
System.out.println("5");
xpp.setInput(in);
System.out.println("6");
int eventType=xpp.getEventType();
System.out.println(eventType+"!!!!!!!!!!!!!!!!");
while(eventType!=XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_DOCUMENT){
System.out.println("start");
}
}
in.close();
return iotd;
}
该程序永远不会打印出“3”,并且 logcat 中有很多警告,我很难理解发生了什么,因为我已经有一段时间没有使用 java 了。有任何想法吗?我正在关注此代码示例...
http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
并试图修改它以解析 xml,但我什至还没有测试那部分。
这是打印“2”后logcat给我的所有警告
07-10 05:31:30.610: W/System.err(534): android.os.NetworkOnMainThreadException
07-10 05:31:30.621: W/System.err(534): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-10 05:31:30.630: W/System.err(534): at java.net.URL.openStream(URL.java:462)
07-10 05:31:30.630: W/System.err(534): at com.wajumbie.nasadailyimage.RssParse.setUrl(RssParse.java:14)
07-10 05:31:30.630: W/System.err(534): at com.wajumbie.nasadailyimage.NasaDailyImage.onCreate(NasaDailyImage.java:29)
07-10 05:31:30.630: W/System.err(534): at android.app.Activity.performCreate(Activity.java:4465)
07-10 05:31:30.630: W/System.err(534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-10 05:31:30.630: W/System.err(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-10 05:31:30.630: W/System.err(534): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-10 05:31:30.640: W/System.err(534): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 05:31:30.640: W/System.err(534): at android.os.Looper.loop(Looper.java:137)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 05:31:30.640: W/System.err(534): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 05:31:30.640: W/System.err(534): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 05:31:30.640: W/System.err(534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 05:31:30.640: W/System.err(534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 05:31:30.640: W/System.err(534): at dalvik.system.NativeStart.main(Native Method)