Skip to content

使用retrofit2+rxjava2+okhttp3封装的网络请求库

Notifications You must be signed in to change notification settings

Hello-Kiki/rrorequest

Repository files navigation

rrorequest

对retrofit2+rxjava2+okhttp3 进行封装使用,尽可能方便的调用
封装功能:普通请求(post,get)
          文件上传(图文,多文件,单文件带进度)
          文件下载(可多文件同时,带进度)

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}
Step 2. Add the dependency

dependencies {
         compile 'com.github.Hello-Kiki:rrorequest:1.1'
}

普通请求调用

       HttpManager.getInstance().create(ApiService.class).getData()
                .compose(HttpManager.<JsonObject>applySchedulers())
                .subscribe(new SimpleCallBack<JsonObject>() {
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        Log.e("2017","成功-->"+jsonObject.toString());
                        mTextView.setText(jsonObject.toString());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("2017","失败-->"+e.toString());
                    }
                });

文件上传

     Map<String,RequestBody> textBody=MultipartUtil.newInstance()
                    .addParam("text1","123")
                    .addParam("text2","456")
                    .Build();

            List<File> files=new ArrayList<>();
            File file=new File(Environment.getExternalStorageDirectory()+"test.png");
            files.add(file);

            //文件上传进度只支持单文件上传的时候使用
             List<MultipartBody.Part> parts= MultipartUtil.makeMultpart("images", files, new ProgressListener() {
              @Override
               public void onProgress(long read, long length, boolean done) {

                }
              });
            HttpManager.getInstance().create(ApiService.class).uploadFile(textBody,parts)
                    .compose(HttpManager.<JsonObject>applySchedulers())
                    .subscribe(new SimpleCallBack<JsonObject>() {
                        @Override
                        public void onSuccess(JsonObject jsonObject) {
                            //请求成功
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            //请求失败
                        }
                    });

文件下载

   因为每个人使用的数据库都不一样,在这里不统一做下载数据的保存,下载信息类为  DownInfo  ,如果需要做保存,自行继承处理。

    private DownInfo mDownInfo;
    private HttpDownManager manager=HttpDownManager.getInstance();

    mDownInfo=new DownInfo();
    mDownInfo.setUrl("xxx");
    mDownInfo.setSavePath("xxx");

      manager.start(mDownInfo, new HttpDownListener() {
                @Override
                public void onStart() {
                    mButtonDown.setText("暂停");
                }

                @Override
                public void onPause(long read) {
                    mDownInfo.setReadLength(read);
                    mButtonDown.setText("继续");
                    mTextViewProgress.setText("下载暂停");
                }

                @Override
                public void onStop(long read) {
                    mDownInfo.setReadLength(read);
                    mButtonDown.setText("下载");
                    mTextViewProgress.setText("下载停止");
                }

                @Override
                public void onFinish(DownInfo info) {
                    mDownInfo=info;
                    mButtonDown.setText("下载");
                    mTextViewProgress.setText("下载成功");
                }

                @Override
                public void onError(DownInfo info,String s) {
                    mDownInfo=info;
                    mButtonDown.setText("下载");
                    mTextViewProgress.setText("下载失败");
                }

                @Override
                public void onProgress(long currentRead, long addLength) {
                    int pro=(int)(currentRead*100/addLength);
                    mProgressBar.setProgress(pro);
                    mTextViewProgress.setText("下载中:"+pro+"%");
                }
            });

          manager.pause(mDownInfo); //暂停下载
         manager.stop(mDownInfo); //停止下载

取消请求

    eg:
             HttpManager.getInstance().create(ApiService.class).getData()
                .compose(HttpManager.<JsonObject>applySchedulers())
                .subscribe(new SimpleCallBack<JsonObject>("123") {      //在构造函数中闯入标记
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        Log.e("2017","成功-->"+jsonObject.toString());
                        mTextView.setText(jsonObject.toString());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        Log.e("2017","失败-->"+e.toString());
                    }
                });

    在new SimpleCallBack<JsonObject>("123")  中增加tag 参数,表示该次请求的标记

   HttpManager.getInstance().cancel("123"); //单个请求取消

   HttpManager.getInstance().cancelAllRequest();   //取消全部请求

其他

    setConnectTimeOut           设置全局连接超时时间(秒)
    setReadTimeOut              设置全局读取超时时间(秒)
    setWriteTimeOut             设置全局写入超时时间(秒)

    DownInfo    下载信息类:
        readLength      //下载进度
        countLength     //下载总长度
        url             //下载url
        savePath        //下载保持地址
        listener        //下载监听器
        state           //下载状态  1:下载中,2:暂停,3:停止,4:完成,5:错误

     DownState  下载状态类

About

使用retrofit2+rxjava2+okhttp3封装的网络请求库

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages