码迷,mamicode.com
首页 > 移动开发 > 详细

Novate 一款Android RxStyle的网络框架

时间:2017-09-25 14:35:50      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:parse   int   art   pil   fileutil   多文件   请求   key值   UI   

功能

  • 优化设计:加入基础API,减少Api冗余

  • 强大的缓存模式: 支持离线缓存, 无网络智能加载缓存,可配置是否需要缓存

  • cookie管理:自带cookie管理机制

  • 全方位请求模式:支持多种方式访问网络(get,put, post ,delete)

  • 轻送调用:支持表单,图文一起,json上传。

  • 文件传输:支持文件下载和上传,支持进度

  • 动态添加:支持请求头和参数统一添加,分别添加。

  • 结果处理:支持对返回结果的统一处理,自动帮你序列化复杂的数据。

  • 扩展性强:支持自定义的Retrofit的API,默认Api无法满足时可自定义自己的Service

  • 悠雅方便:支持统一请求访问网络的流程控制,以方便帮你完美加入Processbar进度。

  • RxJava结合: 结合RxJava,线程智能控制

集成

Gradle:  

  • root:

    repositories { maven { url "https://jitpack.io" } jcenter() }

  • app:

       dependencies {
          compile ‘com.tamic.novate:novate:1.5.4.3‘
       }

 

RxAPi

主要处理请求的API,包含RxGet, RxPost, RxDelete,RxPut, RxBody,RxFrom, RxUpLoad,RxDownLoad.使用基本APi之前 请阅读对RxCallBack的介绍。

 

RxGet

进行get方式的请求调用,多种返回结果的方式供你选择,返回不同的数据类型参考请看原文链接RxCallBack的介绍。


 

基础使用:

返回String

 new Novate.Builder(this)           .baseUrl(“www.xxx.com/”)           .build()           .rxGet("service/path", parameters, new RxStringCallback() {        });

返回Bean

novate.rxGet("path or url", parameters, new RxResultCallback<JavaBean>() {          });

返回List

   new Novate.Builder(this)            .baseUrl("http://xxx.com/")            .build()            .rxGet("service/getList", parameters, new RxListCallback<List<JavaBean>>() {              ...            });

返回File

novate.rxGet("path or url", null, new RxFileCallBack(filePath, "name.jpg") {
   .....    });

 

RxPost:

进行Post方式的请求调用

返回String

  novate.rxPost("path or url", parameters, new RxStringCallback() {    .....          });

返回Bean

novate.rxPost("path or url", parameters, new RxResultCallback<ResultModel>() {  

});

返回List

novate.rxPost("path or url", parameters, new RxListCallback<List<ResultModel>>() {       ....    });

返回File

novate.rxPost("path or url", null, new RxFileCallBack(filePath, "name.jpg") {       ....    });

 

上传文件

 

这里主要介绍怎么使用Novate上传文件:

Novate提供了2种方式上传文件。body和part模式,Body不包含key值,part包含key值。

 

RxUploadWithBody

以Body方式post数据,可以上报文件,图片等。

  String mPath = uploadPath; //"you File path ";    String url = "http:/xxx.com";    novate.rxUploadWithBody(url, new File(mPath), new RxStringCallback() {      ....    }); }

 

RxUploadWithPart

上传文件,默认的key是  image 

  String mPath = uploadPath; //"you File path ";   String url = "http:/xxx.com";   File file = new File(mPath);   novate.rxUploadWithPart(url, file, new RxStringCallback() {      ....    });

 

上传多文件:

 

RxUploadWithPartListByFile:

  List<File> fileList = new ArrayList<>();    fileList.add(file);    fileList.add(file);    fileList.add(file);    novate.rxUploadWithPartListByFile(url, fileList, new RxStringCallback() {                 });

 

图文一起

    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)      .addFormDataPart("key1", V1)      .addFormDataPart("key2", v2)      .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))      .build();   novate.rxBody(url , requestBody, callback);

 

RxBody

  RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)      .addFormDataPart("key1", V1)      .addFormDataPart("key2", v2)      .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))      .build();    novate.rxBody(url , requestBody, callback);

下载文件

 

使用rxGet()实现下载:

 String downUrl = "http://wap.dl.pinyin.sogou.com/wapdl/hole/201512/03/SogouInput_android_v7.11_sweb.apk";  novate.rxGet(downUrl, parameters, new RxFileCallBack(FileUtil.getBasePath(this), "test.apk") {          });

 

RxDown()下载

   String downUrl = "http://wap.dl.pinyin.sogou.com/wapdl/hole/201512/03/SogouInput_android_v7.11_sweb.apk";    new Novate.Builder(this)            .rxDownload(downUrl, new RxFileCallBack(FileUtil.getBasePath(this), "test.apk") {                            });

 

OkHTTP 姿势

喜欢okhtp姿势的朋友可以继续使用姿势:

  Request request =
            new Request.Builder()
                    .get()
                    .url("you url")
                    .build();

    novate.execute(request, new RxStringCallback() {

        
    });

 

Retrofit Api 姿势

Novate默认的API让你不爽时,Novate同样支持你自己Retrofit的ApiService

定义APi 同Retrofit的Api

新建MyApi

 

 public interface MyApi {

  @GET("url")
  Observable<MyBean> getdata(@QueryMap Map<String, String> maps);

 }

Execute

调用Call()

 MyApi myApi = novate.create(MyApi.class);

 novate.call(myApi.getdata(parameters),
            new BaseSubscriber<MyBean>{
            ‘‘‘‘‘‘‘
            });

}

 

Novate 一款Android RxStyle的网络框架

标签:parse   int   art   pil   fileutil   多文件   请求   key值   UI   

原文地址:http://www.cnblogs.com/gongxiaojiu/p/7591489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!