DownloadManager没有正确下载

时间:2015-03-13 11:00:42

标签: java android

我有使用DownloadManager下载文件的Class。

我从MainActivity用1个参数调用这个类,可以是rzeszow,warszawa或krakow。

问题是它没有正确下载这些文件,它们应该有18.4kB但它们有17.9kB,如果我下载文件bus_users这个类它下载正确。这是我的班级

package com.example.buses;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;

class Downloading extends AsyncTask<String, Void, Void> {
    ProgressDialog pDialog = null;
    String city;
    String lastUpdateString;

    private final Context context;
    public Downloading(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
    }

    enter code here
    @Override
    protected Void doInBackground(String... names) {
        String dest_file_path = "/Android/data/com.example.buses/databases/" + User.getCurrentUser() + '/';
        String dwnload_file_path = "http://emy-projects.pp.ua/buses_users/ + names[0];
        DownloadManager mgr=null;
        long lastDownload;
        city = names[0];

        mgr=(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri uri=Uri.parse(dwnload_file_path);
        DownloadManager.Request req=new DownloadManager.Request(uri);
        Uri dst_uri = Uri.parse(dest_file_path);
        Environment.getExternalStoragePublicDirectory(dst_uri.getPath()).mkdirs();
        req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                                       | DownloadManager.Request.NETWORK_MOBILE)
           .setAllowedOverRoaming(false)
           .setTitle("Demo")
           .setDescription("Something useful. No, really.")
           .setDestinationInExternalPublicDir(dst_uri.getPath(),
                                              "buses_database");
        lastDownload=mgr.enqueue(req);
        try{
            URL url = new URL(dwnload_file_path);
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            long date = httpCon.getLastModified();
            lastUpdateString = Long.toString(date);
        }catch (IOException e) {
            return null; 
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        MainActivity.pDialog.dismiss();
        MainActivity.dbHelper.insertNewUser(User.getCurrentUser(), city, lastUpdateString);
        MainActivity.currentCity = city;
    }
  }

0 个答案:

没有答案