[英]Very slow parsing in AVD
我的代碼獲取RSS提要並解析它。 代碼工作正常,但在某些函數中速度很慢(請參閱代碼中的時間)。 有什么建議么?
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
好友,嘗試使用XMLPullParser來解析數據,解析數據不會花費太多時間,難以解決6-7次。 這是代碼。 試試這個:
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;
}
}
我希望這個例子會幫助你。 :)
問題可能是模擬器本身,因為它有性能問題。 我建議您在實際設備上測試它以確定問題是來自模擬器還是來自代碼。
如果您目前無法在設備上進行測試,那么這個問題的答案就會提出許多改進模擬器的提示: 為什么Android模擬器這么慢? 我們如何加快Android模擬器的速度?
您可以使用Genymotion ,它非常快速,您可以通過adb訪問日志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.