【问题标题】:Android doesn't download correct fileAndroid 没有下载正确的文件
【发布时间】:2017-02-19 10:29:29
【问题描述】:

在我的 Android 应用程序中,我有一个从服务器下载文件的按钮。我试图从服务器中删除文件,但是当我按下按钮时,应用程序会下载文件。 这是代码:

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();
    }
}

我认为可能是缓存的问题,然后我添加了这段代码来清除缓存

Clear Application cache on exit in android

但问题依然存在。

【问题讨论】:

  • 在这两种情况下检查内容长度(您最终写入文件的长度)。当找不到文件时,可能还有其他内容被写入文件。还要检查每种情况下的文件大小。它们完全匹配吗?
  • 文件大小更改,现在我删除服务器上的文件但我可以下载旧文件。我不知道为什么它没有显示错误信息
  • 那么您没有正确检查文件是否在服务器上可用,并且在文件不可用时将其他内容写入文件。检查两种情况下的内容,并尽可能将它们包含在您的问题中。
  • 连接的响应码是200。如果我重启手机,应用程序就可以正常工作了

标签: android caching download


【解决方案1】:

我发现了问题!问题是在执行任务时,它确实创建了很多线程。

我变了

downloadTask.execute(params)

downloadTask.executeOnExecutor(Executors.newSingleThreadExecutor(), params)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多