[英]Very slow parsing in AVD
My code gets a RSS feed and parses it. 我的代码获取RSS提要并解析它。 Code works fine, but is very slow in some functions (see time in code). 代码工作正常,但在某些函数中速度很慢(请参阅代码中的时间)。 Any suggestions? 有什么建议么?
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
Buddy , try using XMLPullParser to parse data , it won't take much time to parse data hardly 6-7 secondes. 好友,尝试使用XMLPullParser来解析数据,解析数据不会花费太多时间,难以解决6-7次。 Here is the code . 这是代码。 Try this out: 试试这个:
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;
}
}
I hope this will example will help you out. 我希望这个例子会帮助你。 :) :)
The problem might be the emulator itself, since it has performance issues. 问题可能是模拟器本身,因为它有性能问题。 I suggest that you test it on an actual device to determine whether the problem comes from the emulator or from the code. 我建议您在实际设备上测试它以确定问题是来自模拟器还是来自代码。
If testing on a device is not possible for you at the moment, answers to this SO question suggest many tips to improve the emulator : Why is the Android emulator so slow? 如果您目前无法在设备上进行测试,那么这个问题的答案就会提出许多改进模拟器的提示: 为什么Android模拟器这么慢? How can we speed up the Android emulator? 我们如何加快Android模拟器的速度?
您可以使用Genymotion ,它非常快速,您可以通过adb访问日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.