OkHttpUtils 下载文件
public void downloadFile(){
String url="http://vf2.mtime.cn/Video/2017/03/15/mp4/170315222409670447.mp4";
OkHttpUtils.get()
.url(url)
.build()
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),
"okHttp-test.mp4") {
@Override
public void onError(Call call, Exception e, int id) {
Log.e(TAG, "onError :" + e.getMessage());
}
@Override
public void inProgress(float progress, long total, int id) {
//super.inProgress(progress, total, id);
Log.e(TAG,"inProgress"+(int)(100*progress));
}
@Override
public void onResponse(File file, int id) {
Log.e(TAG, "onResponse :" + file.getAbsolutePath());
}
});
}
这里展示了下载OkHttpUtils的用法,和普通的get()和post()请求没有什么不同,核心还是在于他的回调,毕竟下载的内容是在回调中取到,对调中依然只有onError(),inProgress(),onResponse()方法,FileCallBack的构造方法接收两个参数,一个是文件路径,一个是文件名