【问题标题】:Getting a web page in an abortable process on Android. What is the most responsive way?在 Android 上的可中止进程中获取网页。什么是最响应的方式?
【发布时间】:2024-01-05 05:48:01
【问题描述】:

我正在编写一个需要尽快响应的 Android 应用程序。该应用程序基本上从网站 (HTML, not XHTML) 获取内容,并使用 XPATH 查询解析其中的一些信息。我希望用户能够点击取消并中止请求,返回到上一个活动。

现在我的代码看起来像这样(我使用从http://htmlcleaner.sourceforge.net/convert html to xhtml 的htmlcleaner jar):

   //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;
   }

我不确定我的代码中的下载是否真的可以中止,更不用说我知道如何完成这个了。我是在正确的道路上还是应该采取不同的方法?

【问题讨论】:

  • 顺便说一句,我的代码工作正常,我只需要能够在用户点击“返回”时中止数据传输。

标签: android uri httprequest android-asynctask abort


【解决方案1】:

我已经解决了这个问题,有关使用 ASyncTask 的更多信息,请参阅这篇文章。 Android stop download

【讨论】:

    最近更新 更多