2013-08-16 533 views
1

我的代碼獲取RSS提要並對其進行解析。代碼工作正常,但在一些函數中速度很慢(請參閱代碼中的時間)。有什麼建議麼?AVD中的解析速度非常慢

 URLConnection connection = feedUrl_.openConnection(); 
     InputStream inputStream = connection.getInputStream(); // 15 sec 

     Reader reader = new InputStreamReader(inputStream, "UTF-8"); 
     InputSource inputSource = new InputSource(reader); 
     doc = builder.parse(inputSource); // 60 sec 
+0

它總是給我慢......我寧願執行到設備(使用eclipse) –

回答

2

好友,請嘗試使用XMLPullParser解析數據,也不會花費太多時間來幾乎解析數據6-7 secondes。這是代碼。試試這個:

onCreate() 
{ 
try 
    { 

    URL url = null; 
    url = new URL("http://feeds.abcnews.com/abcnews/worldnewsheadlines"); 
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(false); 
    XmlPullParser xpp = factory.newPullParser(); 
    xpp.setInput(getInputStream(url), "UTF_8"); 
    boolean insideItem = false; 

     // Returns the type of current event: START_TAG, END_TAG, etc.. 
     int eventType = xpp.getEventType(); 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      if (eventType == XmlPullParser.START_TAG) { 

       if (xpp.getName().equalsIgnoreCase("item")) { 
        insideItem = true; 
       } else if (xpp.getName().equalsIgnoreCase("title")) { 
        if (insideItem) 
         headlines.add(xpp.nextText()); // extract the 
                 // headline 
       } else if (xpp.getName().equalsIgnoreCase("link")) { 
        if (insideItem) 
         links.add(xpp.nextText()); // extract the link of 
                // article 
       } 
      } else if (eventType == XmlPullParser.END_TAG 
        && xpp.getName().equalsIgnoreCase("item")) { 
       insideItem = false; 
      } 

      eventType = xpp.next(); // move to next element 
     } 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

     public InputStream getInputStream(URL url) { 
    try { 
     return url.openConnection().getInputStream(); 
    } catch (IOException e) { 
     return null; 
    } 
} 

我希望這個例子能幫助你。 :)

+0

男人,它真的更快! Thanx,將使用它 –

+0

繼續前進! :d –