下载文件时在Android中找不到DownloadManager错误416

时间:2013-05-22 13:56:17

标签: android mysql json android-download-manager

我有一个问题。我想从Web服务器数据库MySQL下载文件。我使用JSON来解析文件的url。但是当我尝试下载时,它总是返回416。 有什么问题?我的代码或如何将文件URL存储在数据库中?

我的错误

05-22 21:22:41.615: WARN/DownloadManager(262): Aborting request for download 15: http error 416

这是我的onCreate代码

super.onCreate(savedInstanceState);

    //cek koneksi internet
    cd = new ConnectionDetector(this);
    IsConnected = cd.isConnectingToInternet();

    Bundle b = getIntent().getExtras();
    konteksCocok = b.getString("kts_cocok");
    Log.d("konteks datang", "data: " + konteksCocok);

    if(IsConnected) {
        new downloadFile().execute(urlBerkas);
    } else {
        showAlertDialog(this, "Koneksi Gagal", "Tidak ada koneksi internet", false);
    }

这是我的json和下载管理器代码

class downloadFile extends AsyncTask<String, String, String> {

    int count;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = new ProgressDialog(unduhBerkas.this);
        progress.setMessage("Mengambil data...");
        progress.setIndeterminate(false);
        progress.setCancelable(false);
        progress.show();
    }

    @Override
    protected String doInBackground(String... xUrl) {
        // TODO Auto-generated method stub
        //mendapatkan data dari database berupa detail berkas
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("konteks", konteksCocok));
        Log.d("konteks kirim", "data: " + konteksCocok);
        JSONObject json = jParser.makeHttpRequest(url_webservice, "POST", params);
        Log.d("data: ", json.toString());

        //menampilkan data lokasi POI
        try {
            int sukses = json.getInt(TAG_SUCCESS);
            if(sukses == 1) {
                POI = json.getJSONArray(TAG_BERKAS);
                for(int i = 0; i < POI.length(); i++) {
                    JSONObject c = POI.getJSONObject(i);
                    nama = c.getString(TAG_NAMA_PENGGUNA);
                    konteks = c.getString(TAG_KONTEKS_BERKAS);
                    urlBerkas = c.getString(TAG_URL_BERKAS);
                    namaBerkas = c.getString(TAG_NAMA_BERKAS);

                    Log.d("namaDB", "data: " + nama);
                    Log.d("ktsDB", "data: " + konteks);
                    Log.d("urlDB", "data: " + urlBerkas);
                    Log.d("berkasDB", "data: " + namaBerkas);
                }

                try {
                    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    String url = "http://10.0.2.2/locRemService/berkas/" + urlBerkas;
                    Request request = new Request(Uri.parse(url));
                    enqueue = dm.enqueue(request
                            .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nama));
                    BroadcastReceiver receiver = new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            String action = intent.getAction();
                            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                                long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                                Query query = new Query();
                                query.setFilterById(enqueue);
                                Cursor c = dm.query(query);
                                if (c.moveToFirst()) {
                                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                                        Log.d("data downloaded", "sukses");
                                    }
                                }
                            }
                        }
                    };
                    registerReceiver(receiver, new IntentFilter(
                            DownloadManager.ACTION_DOWNLOAD_COMPLETE)
                    );

                } catch (Exception e) {
                    Log.e("Error: ", e.getMessage());
                }
            } else {
                Log.d("JSON error", "JSON trouble");
            }
        } catch(JSONException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {
        Toast.makeText(getApplicationContext(), "Unduh Berkas...", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onPostExecute(String result) {
        progress.dismiss();

    }
}

任何人都可以帮忙吗?谢谢你的帮助...

0 个答案:

没有答案