天天看点

android 下载工具类,Android okHttp文件下载并带进度条的demo(简单工具类)

根据okHttp简单的封装了一个根据url下载文件并更新进度的工具类,在此记录下,以后要使用可以进行参考

先来看看效果图:

android 下载工具类,Android okHttp文件下载并带进度条的demo(简单工具类)

接下来看看具体的代码实现

首先在:app中添加项目对于Okhttp的依赖:

//okhttp

compile 'com.squareup.okhttp3:okhttp:3.3.1'

新建Java文件DownloadUtil.java

package com.demo.test;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import okhttp3.Call;

import okhttp3.Callback;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

public class DownloadUtil {

private static DownloadUtil downloadUtil;

private final OkHttpClient okHttpClient;

public static DownloadUtil get() {

if (downloadUtil == null) {

downloadUtil = new DownloadUtil();

}

return downloadUtil;

}

private DownloadUtil() {

okHttpClient = new OkHttpClient();

}

public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {

Request request = new Request.Builder().url(url).build();

okHttpClient.newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

// 下载失败监听回调

listener.onDownloadFailed(e);

}

@Override

public void onResponse(Call call, Response response) throws IOException {

InputStream is = null;

byte[] buf = new byte[2048];

int len = 0;

FileOutputStream fos = null;

// 储存下载文件的目录

File dir = new File(destFileDir);

if (!dir.exists()) {

dir.mkdirs();

}

File file = new File(dir, destFileName);

try {

is = response.body().byteStream();

long total = response.body().contentLength();

fos = new FileOutputStream(file);

long sum = 0;

while ((len = is.read(buf)) != -1) {

fos.write(buf, 0, len);

sum += len;

int progress = (int) (sum * 1.0f / total * 100);

// 下载中更新进度条

listener.onDownloading(progress);

}

fos.flush();

// 下载完成

listener.onDownloadSuccess(file);

} catch (Exception e) {

listener.onDownloadFailed(e);

} finally {

try {

if (is != null)

is.close();

} catch (IOException e) {

}

try {

if (fos != null)

fos.close();

} catch (IOException e) {

}

}

}

});

}

public interface OnDownloadListener {

void onDownloadSuccess(File file);

void onDownloading(int progress);

void onDownloadFailed(Exception e);

}

}

使用方法

public void downFile(String url) {

progressDialog = new ProgressDialog(SplashActivity.this);

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

progressDialog.setTitle("正在下载");

progressDialog.setMessage("请稍后...");

progressDialog.setProgress(0);

progressDialog.setMax(100);

progressDialog.show();

progressDialog.setCancelable(false);

DownloadUtil.get().download(url, Environment.getExternalStorageDirectory().getAbsolutePath(), "kuoke.apk", new DownloadUtil.OnDownloadListener() {

@Override

public void onDownloadSuccess(File file) {

if (progressDialog != null && progressDialog.isShowing()) {

progressDialog.dismiss();

}

//下载完成进行相关逻辑操作

}

@Override

public void onDownloading(int progress) {

progressDialog.setProgress(progress);

}

@Override

public void onDownloadFailed(Exception e) {

//下载异常进行相关提示操作

}

});

}简单的工具封装,仅供参考,大神多多指教,一起进步