0

In my Android app I have a button that downloads a file from a server. I have tried to delete the file from the server but when I push the button the app downloads the file. This is the code:

private class DownloadTask extends AsyncTask<String, Integer, String>
{

    private Context context;
    private PowerManager.WakeLock mWakeLock;

    public DownloadTask(Context context)
   {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... sUrl)
    {
        InputStream input = null;
        OutputStream output = null;
       HttpURLConnection connection = null;

       try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();

            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK){
                return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
            }

           // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream("/sdcard/LabTracKin/analisi");

           byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (output != null)
                   output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // take CPU lock to prevent CPU from going off if the user
        // presses the power button during download
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                getClass().getName());
        mWakeLock.acquire();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // if we get here, length is known, now set indeterminate to false
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        mProgressDialog.dismiss();
        if (result != null)
            Toast.makeText(context, "Download error: " + result, Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context,"File Scaricato", Toast.LENGTH_SHORT).show();
    }
}

I thought it might be a problem with caching, then I added this code to clear the cache

Clear Application cache on exit in android

But the problem persists.

Community
  • 1
  • 1
chianta
  • 123
  • 1
  • 2
  • 12
  • check the content length (that you eventually write to the file) in both cases. probably something else is getting written to the file when it does not find it. also check the file size in each case. do they match exactly? – Viral Patel Dec 23 '15 at 08:00
  • Size of file change, now I delete file on server but I can download old file. I don't know why It not show the error message – chianta Dec 23 '15 at 08:13
  • then you are not checking correctly if the file is available on server or not and writing something else to file when it is not available. Check the contents in both cases and include them in your question if possible. – Viral Patel Dec 23 '15 at 08:15
  • The response code of connection is 200. If I reboot the phone the app works perfectly – chianta Dec 23 '15 at 08:28

1 Answers1

0

I have found the problem! The problem was in execution of task, it did create a lot of threads.

I have changed

downloadTask.execute(params)

in

downloadTask.executeOnExecutor(Executors.newSingleThreadExecutor(), params)
chianta
  • 123
  • 1
  • 2
  • 12