首页 > 代码库 > 通过接口返回数据下载文件的两种返回数据类型方法
通过接口返回数据下载文件的两种返回数据类型方法
/** 下载pdf文件 */
private String updateContentName;//文件名
private Call call;
private boolean isSuccess = true;//是否下载成功
File target = null;
protected void download(String downloadUrl) {
showLoading();
target = new File(Environment.getExternalStorageDirectory() + "/ZyHealthy/PDF", updateContentName);
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
OkHttpClient client = new OkHttpClient();
//构造请求
FormBody body = new FormBody.Builder()
.build();
final Request request1 = new Request.Builder()
.url(downloadUrl)
.post(body)
.build();
//包装Response使其支持进度回调
call = ProgressHelper.addProgressResponseListener(client, uiProgressResponseListener).newCall(request1);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
DesityUtil.showToast("下载失败,请尝试重新下载"+e);
closeLoading();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// //方案一 String型的byte数组
// int n = 1024;
// String bodyStr = response.body().string();
// try {
// String decodedString = new String(Base64Utils.decode(bodyStr));
// byte[] bytes = gson.fromJson(decodedString, new TypeToken<byte []>(){}.getType());
// FileOutputStream fos = new FileOutputStream(target, true);
// int length = bytes.length;
// int start = 0;
// while (length > start+n) {
// fos.write(bytes, start, n);
// start = start+n;
// }
// if (length != start+n) {
// n = length-start;
// fos.write(bytes, start, n);
// }
// fos.flush();
// fos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
//方案二 io流
InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(target, true);
byte[] buffer = new byte[2048];//缓冲数组2kB
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
is.close();
fos.close();
closeLoading();
if (target.exists()) {
dialogFragment = new PromptDialogFragment();
dialogFragment
.setTitle(getResources().getString(R.string.tjbb_exportPDF) + " /ZyHealthy/PDF/" + updateContentName)
.setMissVisibility(View.GONE)
.setSureOnClickListener(new NoDoubleClickListener() {
@Override
protected void onNoDoubleClick(View v) {
dialogFragment.dismiss();
}
});
dialogFragment.show(getFragmentManager(), "FullScreenDownPDFFragment");
} else {
DesityUtil.showToast("下载失败,请尝试重新下载");
}
}
});
}
//这个是ui线程回调,可直接操作UI
PromptDialogFragment dialogFragment;
UIProgressListener uiProgressResponseListener = new UIProgressListener() {
@Override
public void onUIProgress(long bytesRead, long contentLength, boolean done) {
isSuccess = done;
NumberFormat formatter = new DecimalFormat("00.#");
// String format = formatter.format(Float.valueOf(bytesRead) / contentLength * 100);
// 下载完成 弹框提示
if (100 <= (Float.valueOf(bytesRead) / contentLength * 100)) {
// DesityUtil.showToast("下载完成");
}
}
@Override
public void onUIStart(long bytesRead, long contentLength, boolean done) {
super.onUIStart(bytesRead, contentLength, done);
}
@Override
public void onUIFinish(long bytesRead, long contentLength, boolean done) {
super.onUIFinish(bytesRead, contentLength, done);
isSuccess = done;
}
};
public void showLoading() {
if (loadingFragment != null)
loadingFragment.show(getFragmentManager(), "loading");
}
public void closeLoading() {
if (loadingFragment != null)
loadingFragment.dismiss();
}
通过接口返回数据下载文件的两种返回数据类型方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。