首页 > 解决方案 > Volley 或 Fast Networking 不会在特定链接中下载整个文件(损坏的文件)

问题描述

我有从 api 下载文件的代码。当我提供的网址类似于“ http://blalba.towert.win/core/me/service.php?class=awin%2Fcore%2Fmod%2Fresource%2Fview.php%3Fid%3D5&id=3 ”时,我遇到了问题我得到了损坏的文件。

但是当使用来自 Dropbox 的直接链接时,例如 https://www.dropbox.com/s/b5v2asdasd9bly663qr/thisFile.pdf?dl=1 可以正确下载而不会损坏。

如何使用第一个链接正确下载?

我的快速网络代码:

new FastNetworkingUtils().FNRequestDownloadAPKFile(activity, url, txtPathUserData, pdfName, TAG_DOWNLOAD_PDF, dialog, new InterfaceFastNetworkingDownloadFile() {
            @Override
            public void onProgress(final long bytesDownloaded, final long totalBytes) {
                // do anything with progress
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        double precentage = ((double) bytesDownloaded / (double) totalBytes) * 100;
//                        progressD.setProgress((int) precentage);
                    }
                });
            }

            @Override
            public void onDownloadComplete() {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        String txtPath = txtPathUserData + pdfName+".pdf";
                        try {
                            File file = new File(txtPath);
                            Uri uri = FileProvider.getUriForFile(activity.getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
                            IntentCustom.intentPDViewer(activity, uri, true);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
            }

            @Override
            public void onError(ANError error) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                    }
                });
            }
        });

我的排球代码:

InputStreamVolleyRequest  request = new InputStreamVolleyRequest(Request.Method.GET, mUrl,
                new Response.Listener<byte[]>() {
                    @Override
                    public void onResponse(byte[] response) {
                        // TODO handle the response
                        try {
                            if (response!=null) {

                                String path = txtPathUserData + pdfName+".pdf";
                                FileOutputStream stream = new FileOutputStream(path);
                                stream.write(response);

                                try {
                                    File file = new File(path);
                                    Uri uri = FileProvider.getUriForFile(activity.getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
                                    IntentCustom.intentPDViewer(activity, uri, true);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                Toast.makeText(context, "Download complete.", Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                            e.printStackTrace();
                        }
                    }
                } ,new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO handle the error
                error.printStackTrace();
            }
        }, null);
        RequestQueue mRequestQueue = Volley.newRequestQueue(context, new HurlStack());
        mRequestQueue.add(request);

标签: androiddownload

解决方案


推荐阅读