1

我有个问题。我想从 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();

    }
}

有人可以帮忙吗??谢谢您的帮助...

4

0 回答 0