在 Android 上的可中止进程中获取网页。最灵敏的方式是什么?
我正在为 Android 编写一个应用程序,需要尽可能快地响应。该应用程序基本上从网站(HTML,而不是 XHTML
)获取内容,并使用 XPATH
查询解析其中的一些信息。我希望用户能够点击取消并中止请求,返回到之前的活动。
现在我的代码看起来像这样(我使用 http://htmlcleaner.sourceforge.net/ 中的 htmlcleaner jar to convert html to xhtml
):
//this is called from the doInBackground() method embedded in an ASyncTask
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
try {
URL url = new URL("---my-url-goes-here---");
URLConnection conn = url.openConnection();
TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
return node;
} catch (Exception e) {
failed = true;
}
我不确定我的代码中的下载是否实际上可以中止,更不用说我知道如何完成此操作。我的方法正确还是应该采取不同的方法?
I am writing an application for Android that needs to respond as fast as possible. The app basically grabs content from a website (HTML, not XHTML
) and parses some of its information using XPATH
queries. I want the user to be able to hit cancel and abort the request, returning to the previous activity.
Right now my code looks like this (I use the htmlcleaner jar from http://htmlcleaner.sourceforge.net/ to convert html to xhtml
):
//this is called from the doInBackground() method embedded in an ASyncTask
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
try {
URL url = new URL("---my-url-goes-here---");
URLConnection conn = url.openConnection();
TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
return node;
} catch (Exception e) {
failed = true;
}
I am not sure if the downloading in my code is actually abortable, let alone that I would know how to accomplish this. Am I on the right way or should I go for a different approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题,请参阅这篇文章以获取有关使用 ASyncTask 的更多信息。 Android 停止下载
I've solved this, see this post for more information on using ASyncTask. Android stop download