OkGo上传文件和下载文件

3 篇文章 0 订阅

一.下载文件

     String apkName = TimeUtil.getSystem() + ".apk";//下载APK的名字,目前名字暂定为时间,等和后台确认协议后再修改
        WaitDialog.Builder builder = new WaitDialog.Builder(this).setMessage("准备下载");
        BaseDialog dialog = builder.show();
        String url = "https://pack.qiyutianxia.com/gamepacks/S-wxcqbfb.apk";//一个可以下载APK测试网址
        OkGo.<File>get(url).tag(this).execute(new FileCallback(DirKeys.DIR_APP_UPDATE, apkName) {

            @Override
            public void onSuccess(Response<File> response) {
                builder.setMessage("下载完成");
                dialog.dismiss();
                /*下载完成后直接安装*/
                AppUtils.installApp(response.body());
            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                ToastUtils.error(getString(R.string.apkerror, response.message()));
                dialog.dismiss();
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                builder.setMessage("下载进度\n" + progress.currentSize / BasicKeys.KB + "KB/" + progress.totalSize / BasicKeys.KB + "KB");
            }
        });

二.BasicKeys

public class BasicKeys {

    /******************** 存储相关常量 ********************/
    /**
     * Byte与Byte的倍数
     */
    public static final int BYTE = 1;
    /**
     * KB与Byte的倍数
     */
    public static final int KB = 1024;
    /**
     * MB与Byte的倍数
     */
    public static final int MB = 1048576;
    /**
     * GB与Byte的倍数
     */
    public static final int GB = 1073741824;
    /**
     * 毫秒与毫秒的倍数
     */
    public static final int MSEC = 1;

    /******************** 时间相关常量 ********************/
    /**
     * 秒与毫秒的倍数
     */
    public static final int SEC = 1000;
    /**
     * 分与毫秒的倍数
     */
    public static final int MIN = 60000;
    /**
     * 时与毫秒的倍数
     */
    public static final int HOUR = 3600000;
    /**
     * 天与毫秒的倍数
     */
    public static final int DAY = 86400000;

    /******************** 常量 ********************/
    public static final int KEY_0 = 0;
    public static final int KEY_1 = 1;
    public static final int KEY_2 = 2;
    public static final int KEY_3 = 3;
    public static final int KEY_4 = 4;
    public static final int KEY_5 = 5;
    public static final int KEY_6 = 6;
    public static final int KEY_7 = 7;
    public static final int KEY_8 = 8;
    public static final int KEY_9 = 9;
    public static final int KEY_10 = 10;
    public static final int KEY_11 = 11;
    public static final int KEY_12 = 12;
    public static final int KEY_13 = 13;
    public static final int KEY_14 = 14;
    public static final int KEY_15 = 15;
    public static final int KEY_16 = 16;
    public static final int KEY_17 = 17;
    public static final int KEY_18 = 18;
    public static final int KEY_19 = 19;
    public static final int KEY_20 = 20;
    public static final int KEY_200 = 200;
    public static final int KEY_500 = 500;
    public static final int KEY_404 = 404;

    public static final String KEY_NO_SPACE = "";
    public static final String KEY_ONE_SPACE = " ";
    public static final String KEY_SEPARATOR= "/";
    public static final String KEY_NEXT= "\n";
    public static final String KEY_QUESTION = "?";
    public static final String KEY_AND = "&";
    public static final String KEY_COLON= ": ";
    public static final String KEY_EQUAL= "=";
    public static final String KEY_UTF8= "UTF-8";
    public static final String KEY_NULL= "数据为空";
    public static final String KEY_USE_API= "调用网络接口";
    public static final String KEY_NOT_EXIST= "不存在";
    public static final String KEY_DATA_ERROR= "传入参数错误!";
    public static final String KEY_EXCEPTION= "出现异常";
    public static final String KEY_NOT_NET= "无网络";
    public static final String KEY_ACTION_MAIN="android.intent.action.MAIN";
    public static final String KEY_FILE = "file";
    public static final String HTTP = "http://";
    public static final String COLON = ":";
    public static final String COMMA = ",";
}

三.上传文件

实战:

        WaitDialog.Builder builder = new WaitDialog.Builder(mActivity).setMessage("视频上传");
        builder.show();
        File file = new File(url);
        if (mActivity instanceof InspectionActivity) {
            InspectionActivity inspectionActivity = (InspectionActivity) mActivity;
            lineNumber = inspectionActivity.getLineNumber();
        }
        OkGo.<BaseResult<Void>>post(Api.getuploadVideoUrl())
                .params("file", file)
                .params(Api.TEST_LINE_NUMBER, lineNumber)
                // TODO: 2020-10-27 暂时写死,用于测试
                .params(Api.SERIAL_NUMBER, "3604252010220002")
                .params(Api.CUR_TEST_TIMES, "1")
//                .params(Api.SERIAL_NUMBER, carInfo.getSerialNumber())
//                .params(Api.CUR_TEST_TIMES, carInfo.getTestingTimes())
                .params("item ", item.getCode())//视频代号
                .execute(new JsonCallback<BaseResult<Void>>() {
                    @Override
                    public void TokenOut() {

                    }

                    @Override
                    public void onSuccess(Response<BaseResult<Void>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess()) {
                            builder.setMessage("视频上传成功").dismiss();
                            item.setUrl(url);
                            item.setUploadStatus(1);
                            videoItemAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.error(getString(R.string.uploaderror, response.message()));
                            builder.dismiss();
                            item.setUploadStatus(2);
                            videoItemAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResult<Void>> response) {
                        super.onError(response);
                        ToastUtils.error(getString(R.string.uploaderror, response.message()));
                        builder.dismiss();
                        item.setUploadStatus(2);
                        videoItemAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void uploadProgress(Progress progress) {
                        super.uploadProgress(progress);
                        //上传进度
                        builder.setMessage("上传进度\n" + progress.currentSize / BasicKeys.KB + "KB/" 
+ progress.totalSize / BasicKeys.KB + "KB");
                    }
                });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值