Reputation: 743
I want to cancel downloaded File with Okio ,it runs in AsyncTaks When I cancel Asynctask downloadedFilerunTask.cancel() I can get rid of downlaoding process with using break, but downloaded process not cancelled,how can I cancel downloaded stream and stopped AsynTask in android
@Override
protected String doInBackground(String... strings) {
for(int i=0; i<fileList.size(); i++) {
if (Utils.getStatusAbort()) {
request = new Request.Builder().url(fileList.get(i))
.addHeader("Authorization", header)
.addHeader("Content-Type", "application/json")
.build();
try {
okhttp3.Response response = client.newCall(request).execute();
ResponseBody body = response.body();
int finalI = i;
ZipFile zipFile;
if (response.isSuccessful()) {
try {
Log.i("body", "" + body);
destFilePath = Utils.getDestFilePath(context, fileNameList.get(i));
downloadedFile = new File(destFilePath, fileNameList.get(i));
BufferedSink sink = Okio.buffer(Okio.sink(downloadedFile));
if (response.body() != null) {
sink.writeAll(response.body().source());
}
sink.close();
Utils.savedStateOfApp(context, "publish");
//Log.i("md5:" + md5List.get(i), " = " + Utils.calculateMD5(new File(Utils.getDestFilePath(context, fileNameList.get(i)) + "/" + fileNameList.get(i))));
} catch (Exception e) {
e.printStackTrace();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
Upvotes: 0
Views: 276
Reputation: 61
You need to call cancel()
on the Call
object you created with client.newCall(request)
. Implement the onCancelled()
method in AsyncTask:
@Override
protected void onCancelled() {
if (call != null) {
call.cancel();
}
}
Upvotes: 0