为什么URL.openStream()方法会产生IOException?

时间:2012-04-15 15:04:25

标签: android parsing

大家好我是新的xml解析当我从url获取xml并调用url.openStream()方法IO异常产生任何人帮助我..

SAXParserFactory spf =SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL("http://api.androidhive.info/music/music.xml");
        DataHandler mdaDataHandler = new DataHandler();
        xr.setContentHandler(mdaDataHandler);
         InputStream io = sourceUrl.openStream();//exception is here
        xr.parse(new InputSource(io));
        mmusic = mdaDataHandler.getParseData();

例外是这个

04-15 15:07:06.310: W/System.err(590): java.net.SocketException: Permission denied
04-15 15:07:06.440: W/System.err(590):  at      
org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
04-15 15:07:06.440: W/System.err(590):  at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
04-15 15:07:06.450: W/System.err(590):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
04-15 15:07:06.460: W/System.err(590):  at java.net.Socket.checkClosedAndCreate(Socket.java:873)
04-15 15:07:06.460: W/System.err(590):  at java.net.Socket.connect(Socket.java:1020)
04-15 15:07:06.470: W/System.err(590):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
04-15 15:07:06.470: W/System.err(590):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
04-15 15:07:06.480: W/System.err(590):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
04-15 15:07:06.490: W/System.err(590):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
04-15 15:07:06.490: W/System.err(590):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
04-15 15:07:06.500: W/System.err(590):  at java.net.URL.openStream(URL.java:653)
04-15 15:07:06.500: W/System.err(590):  at com.listview.experiment.CustomListViewActivity.parseXML(CustomListViewActivity.java:56)
04-15 15:07:06.510: W/System.err(590):  at com.listview.experiment.CustomListViewActivity.access$0(CustomListViewActivity.java:43)
04-15 15:07:06.520: W/System.err(590):  at com.listview.experiment.CustomListViewActivity$1.onClick(CustomListViewActivity.java:34)
04-15 15:07:06.520: W/System.err(590):  at android.view.View.performClick(View.java:2408)
04-15 15:07:06.520: W/System.err(590):  at android.view.View$PerformClick.run(View.java:8816)
04-15 15:07:06.530: W/System.err(590):  at android.os.Handler.handleCallback(Handler.java:587)
04-15 15:07:06.540: W/System.err(590):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-15 15:07:06.550: W/System.err(590):  at android.os.Looper.loop(Looper.java:123)
04-15 15:07:06.550: W/System.err(590):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-15 15:07:06.550: W/System.err(590):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 15:07:06.560: W/System.err(590):  at java.lang.reflect.Method.invoke(Method.java:521)
04-15 15:07:06.570: W/System.err(590):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 15:07:06.570: W/System.err(590):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 15:07:06.570: W/System.err(590):  at dalvik.system.NativeStart.main(Native Method)

清单是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.sam.testParsing"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-sdk android:minSdkVersion="8" />
   <application
     android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
       <activity
          android:label="@string/app_name"
           android:name=".XMLParsingActivity" >
              <intent-filter >
                   <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
                   </activity>
               </application>
           </manifest>

2 个答案:

答案 0 :(得分:3)

您无法在UI线程上调用Url.openStream()。使用AsyncTask执行此操作,并在完成后正确更新UI。 `

答案 1 :(得分:1)

您需要将其添加到清单文件中:

<uses-permission android:name="android.permission.INTERNET">

确保它位于<manifest>标记下方。