0
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)
4

3 回答 3

2

有一个NetworkOnMainThreadException。阅读此链接以获取更多信息。还有这篇文章

于 2012-07-10T06:25:21.123 回答
2

异常堆栈跟踪表明您正在主线程上运行它。是这样吗?Android 开发的主线程不应具有网络或磁盘访问权限。您应该将网络操作移至线程,然后严格模式保护不会导致异常。

于 2012-07-10T06:28:11.787 回答
0

试试这个方法

 InputStream is=callWebservice("http://www.nasa.gov/rss/image_of_the_day.rss");
 BufferedReader in=new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"));

其中callWebservice如下图

 private InputStream callWebservice(String serviceURL) {
        HttpClient client=new DefaultHttpClient();
      HttpGet getRequest=new HttpGet();

      try {
          // construct a URI object
          getRequest.setURI(new URI(serviceURL));
      } catch (URISyntaxException e) {
          Log.e("URISyntaxException", e.toString());
      }

      // buffer reader to read the response
      BufferedReader in=null;
      // the service response
      HttpResponse response=null;
      try {
          // execute the request
          response = client.execute(getRequest);
      } catch (ClientProtocolException e) {
          Log.e("ClientProtocolException", e.toString());
      } catch (IOException e) {
          Log.e("IO exception", e.toString());
      }
      if(response!=null)
            try {
                return response.getEntity().getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        else
          return null;
        return null;

    }
于 2012-07-10T06:26:07.833 回答