中止Jsoup请求

时间:2018-01-12 20:25:12

标签: android android-asynctask jsoup abort

我使用Jsoup下载了一个简单的AsyncTask。现在我希望能够在用户退出活动时中止请求,但我没有找到任何方法来执行此操作。这是我的代码:

public class JsoupDownloader extends AsyncTask<String, Integer, Document> {

    public enum AsyncResponseType {
        TYPE_A,
        TYPE_B
    }
    private GenericAsyncResponse delegate = null;
    private AsyncResponseType responseType;
    private Connection connection;

    public JsoupDownloader(GenericAsyncResponse delegate, AsyncResponseType responseType){
        this.delegate = delegate;
        this.responseType = responseType;
    }

    @Override
    protected Document doInBackground(String... urls) {
        Document doc = null;
        try {
            connection = Jsoup.connect(urls[0]);
            doc = connection
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
                    .get();
        } catch (IOException ex){
            Log.e("JosoupDownloader", "IOException when downloading "+urls[0]);
        }
        return doc;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        //setProgressPercent(progress[0]);
    }

    @Override
    protected void onPostExecute(Document response) {
        delegate.processFinish(response, responseType);
    }
}

我知道,例如,课程HttpURLConnection的方法为disconnect()。是否有类似的东西让Jsoup停止下载?

0 个答案:

没有答案