说到网络请求框架Xutils3.0的便利性还是可圈可点的,上传文件大小支持2G。今天为大家介绍一下下载的使用,以下载apk并安装为例,代码如下:
- privatevoiddownload(View v){
- url = "http://127.0.0.1/server/ABC.apk";
- RequestParams params = newRequestParams(url);
- //自定义保存路径,Environment.getExternalStorageDirectory():SD卡的根目录
- params.setSaveFilePath(Environment.getExternalStorageDirectory()+"/myapp/");
- //自动为文件命名
- params.setAutoRename(true);
- x.http().post(params, newCallback.ProgressCallback<file>() {
- @Override
- publicvoidonSuccess(File result) {
- //apk下载完成后,调用系统的安装方法
- Intent intent = newIntent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive");
- getActivity().startActivity(intent);
- }
- @Override
- publicvoidonError(Throwable ex, booleanisOnCallback) {
- }
- @Override
- publicvoidonCancelled(CancelledException cex) {
- }
- @Override
- publicvoidonFinished() {
- }
- //网络请求之前回调
- @Override
- publicvoidonWaiting() {
- }
- //网络请求开始的时候回调
- @Override
- publicvoidonStarted() {
- }
- //下载的时候不断回调的方法,可在该回调方法中获取下载进度。
- @Override
- publicvoidonLoading(longtotal, longcurrent, booleanisDownloading) {
- //当前进度和文件总大小
- Log.i("JAVA","current:"+ current +",total:"+total);
- }
- });
- }
|