通過接口返回數據下載文件的兩種返回數據類型方法


/** 下載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();
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM