android - 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);
解决方案
推荐阅读
- c# - 为什么这会删除所有其他复选框?
- node.js - Angular Cli 6.2.x 分支源,npm install error 'npm audit fix'
- c - 擦除 char*,同时保留字符串
- javascript - javascript模式未在点击功能上显示
- loops - 我的程序一直运行到 if 语句,并且不会进入其中的任何语句。谁能解释为什么会发生这种情况?
- ios - Firebase 身份验证 iOS 12 无效令牌
- php - preg_replace 内容中的图像 src 时出错
- ssh-keygen - ssh-keygen 忽略 -t 参数
- ruby - 如何在 Ruby 中将字符转换为位
- xcode - 更新到 Mojave 后 Xcode 模拟器列表丢失 (9.4.1)