时间:2020-12-19 11:11:42 | 栏目:Android代码 | 点击:次
上传文件的核心功能主要是在UploadCommand.java中实现
使用步骤:
1 创建UploadCommand的构建类Builder
UploadCommand.Builder builder = new UploadCommand.Builder();
2 通过构建类设置UploadCommand的各种属性
builder.domain("上传地址的域名") .path("上传接口") .contentType("请求头文件的Content-type") .mediaType(Params.Body.MEDIA_TYPE_VIDEO) .file("上传文件的绝对路径") .transferListener(new SimpleRequestBody.ProgressListener() { // 上传进度的监听器 @Override public void onDataTransferred(long transferred, long contentLength) { Log.e("TAG", "already transfer " + 100 * ((double) transferred / (double) contentLength)); } });
3 通过构建类构造UploadCommand对象,并启动
UploadCommand uploadCommand = builder.build(); uploadCommand.start(this, new AppResultReceiver(new Handler(), MainActivity.this));
注意:以上代码需要将MainActivity实现AppResultReceiver.ResultListener接口
4 在onResultSuccess方法中,区分是否是UploadCommand,并解析数据
@Override public void onResultSuccess(Bundle resultData) { if (null != resultData) { if (UploadCommand.UPLOAD_CMD.equals( resultData.getString(Params.CommandMessage.CMD_CODE))) { Logger.e("JIANG", "上传成功"); } } }
框架github地址: SimpleCommand框架