I made a simple AsyncTask
to download a webpage using Jsoup. Now I would like to be able to abort the request if the user exits from the activity, but I didn't find any method to do this. This is my code:
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);
}
}
I know that for example the class HttpURLConnection
has the method disconnect()
. Is there something like that for Jsoup to stop the download?