RxJava+Retrofit+OkHttp实现文件上传

所属分类: 软件编程 / Android 阅读数: 29
收藏 0 赞 0 分享

背景

在实际运用中上传是一个必不可少的功能,所以我们在封装二的基础上加入上传的功能,同时需要附带上传进度!

效果

实现

1.定义service接口

注意:Multipart是指定大文件上传过程中的标示,一般上传图片的过程中我们需要附带信息,所以我们需要用到@part指定传递的数值,MultipartBody.Part是指定传递的文件;

  /*上传文件*/
  @Multipart
  @POST("AppYuFaKu/uploadHeadImg")
  Observable<BaseResultEntity<UploadResulte>> uploadImage(@Part("uid") RequestBody uid, @Part("auth_key") RequestBody auth_key,@Part MultipartBody.Part file);

2.加入进度条

retrofit是基于okhttp的处理,所以我们可以自定义RequestBody,复写writeTo(BufferedSink sink)方法,得到传递的进度数据

public class ProgressRequestBody extends RequestBody {
  //实际的待包装请求体
  private final RequestBody requestBody;
  //进度回调接口
  private final UploadProgressListener progressListener;
  //包装完成的BufferedSink
  private BufferedSink bufferedSink;

  public ProgressRequestBody(RequestBody requestBody, UploadProgressListener progressListener) {
    this.requestBody = requestBody;
    this.progressListener = progressListener;
  }
  /**
   * 重写调用实际的响应体的contentType
   * @return MediaType
   */
  @Override
  public MediaType contentType() {
    return requestBody.contentType();
  }
  /**
   * 重写调用实际的响应体的contentLength
   * @return contentLength
   * @throws IOException 异常
   */
  @Override
  public long contentLength() throws IOException {
    return requestBody.contentLength();
  }
  /**
   * 重写进行写入
   * @param sink BufferedSink
   * @throws IOException 异常
   */
  @Override
  public void writeTo(BufferedSink sink) throws IOException {
    if (null == bufferedSink) {
      bufferedSink = Okio.buffer(sink(sink));
    }
    requestBody.writeTo(bufferedSink);
    //必须调用flush,否则最后一部分数据可能不会被写入
    bufferedSink.flush();
  }
  /**
   * 写入,回调进度接口
   * @param sink Sink
   * @return Sink
   */
  private Sink sink(Sink sink) {
    return new ForwardingSink(sink) {
      //当前写入字节数
      long writtenBytesCount = 0L;
      //总字节长度,避免多次调用contentLength()方法
      long totalBytesCount = 0L;
      @Override
      public void write(Buffer source, long byteCount) throws IOException {
        super.write(source, byteCount);
        //增加当前写入的字节数
        writtenBytesCount += byteCount;
        //获得contentLength的值,后续不再调用
        if (totalBytesCount == 0) {
          totalBytesCount = contentLength();
        }
        Observable.just(writtenBytesCount).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
          @Override
          public void call(Long aLong) {
            progressListener.onProgress(writtenBytesCount, totalBytesCount);
          }
        });
      }
    };
  }
}

3自定义接口,回调progress进度

public interface UploadProgressListener {
  /**
   * 上传进度
   * @param currentBytesCount
   * @param totalBytesCount
   */
  void onProgress(long currentBytesCount, long totalBytesCount);
}

4创建RequestBody对象,加入进度

 File file=new File("/storage/emulated/0/Download/11.jpg");
   RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpeg"),file);
   MultipartBody.Part part= MultipartBody.Part.createFormData("file_name", file.getName(), new ProgressRequestBody(requestBody,
       new UploadProgressListener() {
     @Override
     public void onProgress(long currentBytesCount, long totalBytesCount) {
       tvMsg.setText("提示:上传中");
       progressBar.setMax((int) totalBytesCount);
       progressBar.setProgress((int) currentBytesCount);
     }
   }));

5.传递附带信息

和封装二中post请求的方式一样,我们需要继承baseentity,复写里面的方法,然后设置需要传递的参数,因为是测试接口,所以我的参数直接写死在entity里面,part文件动态指定

/**
 * 上传请求api
 * Created by WZG on 2016/10/20.
 */

public class UplaodApi extends BaseEntity {
  /*需要上传的文件*/
  private MultipartBody.Part part;


  public UplaodApi(HttpOnNextListener listener, RxAppCompatActivity rxAppCompatActivity) {
    super(listener, rxAppCompatActivity);
    setShowProgress(true);
  }

  public MultipartBody.Part getPart() {
    return part;
  }

  public void setPart(MultipartBody.Part part) {
    this.part = part;
  }

  @Override
  public Observable getObservable(HttpService methods) {
    RequestBody uid= RequestBody.create(MediaType.parse("text/plain"), "4811420");
    RequestBody key = RequestBody.create(MediaType.parse("text/plain"), "21f8d9bcc50c6ac1ae1020ce12f5f5a7");
    return methods.uploadImage(uid,key,getPart());
  }
}

6.post请求处理

请求和封装二中的请求一样,通过传递一个指定的HttpOnNextListener 对象来回调来监听结果信息,一一对应

 private void uploadeDo(){
   File file=new File("/storage/emulated/0/Download/11.jpg");
   RequestBody requestBody=RequestBody.create(MediaType.parse("image/jpeg"),file);
   MultipartBody.Part part= MultipartBody.Part.createFormData("file_name", file.getName(), new ProgressRequestBody(requestBody,
       new UploadProgressListener() {
     @Override
     public void onProgress(long currentBytesCount, long totalBytesCount) {
       tvMsg.setText("提示:上传中");
       progressBar.setMax((int) totalBytesCount);
       progressBar.setProgress((int) currentBytesCount);
     }
   }));
   UplaodApi uplaodApi = new UplaodApi(httpOnNextListener,this);
   uplaodApi.setPart(part);
   HttpManager manager = HttpManager.getInstance();
   manager.doHttpDeal(uplaodApi);
 }


  /**
   * 上传回调
   */
  HttpOnNextListener httpOnNextListener=new HttpOnNextListener<UploadResulte>() {
    @Override
    public void onNext(UploadResulte o) {
      tvMsg.setText("成功");
      Glide.with(MainActivity.this).load(o.getHeadImgUrl()).skipMemoryCache(true).into(img);
    }

    @Override
    public void onError(Throwable e) {
      super.onError(e);
      tvMsg.setText("失败:"+e.toString());
    }

  };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多