zoukankan      html  css  js  c++  java
  • 通过接口返回数据下载文件的两种返回数据类型方法

    /** 下载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();
    }
  • 相关阅读:
    某个应用使cpu使用率100%
    cpu上下文切换(下)
    kafka集群安装和kafka-manager
    cpu上下文切换
    oralce 记一次 External Procedure initial connection 处理
    Oracle 监听
    Oracle 序列
    Oracle 同义词
    发布到远程存储库时遇到错误: Git failed with a fatal error.
    报表加入参数
  • 原文地址:https://www.cnblogs.com/achen0502/p/7272708.html
Copyright © 2011-2022 走看看