下载的文件为空

时间:2016-05-26 18:51:29

标签: android android-asynctask android-file android-download-manager is-empty

我创建了一个异步任务,可以从网络服务器下载.csv文件。不幸的是,该文件存储在正确的目录中,但它是空的。

这是我的异步任务

public class DownloadTask extends AsyncTask<String, Integer, String> {
    private Context context;
    private ProgressDialog prgDialog = null;
    private PowerManager.WakeLock mWakeLock;
    private String fileName;

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

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

        prgDialog = new ProgressDialog(context);
        prgDialog.setMessage(context.getString(R.string.prgDialogMessage));
        prgDialog.setCancelable(false); // Not able to cancel until programmatically called
        prgDialog.show();
    }

    @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, to make sure the app doesn't mistakenly save an error report
               instead of the file */
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return R.string.errorServer + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // Download the file
            input = connection.getInputStream();

            // Get dynamic storage directory
            File myFile= new File(Environment.getExternalStorageDirectory(), fileName);
            output = new FileOutputStream(myFile);
        } 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 onPostExecute(String result) {
        mWakeLock.release();

        if (prgDialog != null) {
            if (prgDialog.isShowing()) {
                prgDialog.dismiss();
            }
            prgDialog = null;
        }

        if (result != null) {
            Toast.makeText(context,R.string.errorDownload + result, Toast.LENGTH_LONG).show();
        }
    }
}

我没有收到任何错误消息或例外情况,因此我不知道问题出在哪里。我将URL与async任务的execute()函数一起交付。 谢谢你的帮助!

0 个答案:

没有答案