a safety client by Https for Android, (Android网路库,基于Retrofit和RxJava打的的链式网络库, 支持okhttp的调用风格,又兼容Retrofit注解方式,并支持rxJava链式操作。方便扩展,并能实现高速加载)
- Join based API, reduce API redundancy
- Offline caching
- Support a variety of ways to access the network (a get, put, post, delete)
- Support file download and upload
- Unified support request header to join
- The unity of the support to return the result
- Support custom extensions API
- Support the unified request access to the network flow control
#dependencies
Eclipse:
Download the laster JAR:( com.tamic.novate:novate:-1.x.aar)
copy to libs dirPath!
AS Gradle:
-
root:
repositories { maven { url "https://jitpack.io" } jcenter() }
-
app:
dependencies { .....
compile 'com.tamic.novate:novate:1.x.x(laster version)'
}
Snapshots of the development version are available in Sonatype's snapshots repository.
Retrofit requires at minimum Java 7 or Android 2.3.
last vension: https://bintray.com/neglectedbyboss/maven/Novate
基于Retrofit和RxJava封装的链式网络库, 支持okhttp的调用分格式,又兼容Retrofit注解方式,并支持rxJava链式操作,
并于扩展,并能实现高速加载! 为何起名 Novate?
Novate的英文原意是用新事物代替 我的目的是用新的东西来代替Retrofit的有些不易操作的地方,因此起名新奇的东西,所以结合了原来的Http用法习惯,又加入了Retrofit的特性,因此起名 :Novate
- 加入基础API,减少Api冗余
- 支持离线缓存
- 支持多种方式访问网络(get,put,post ,delete)
- 支持Json字符串,表单提交
- 支持文件下载和上传
- 支持请求头统一加入
- 支持对返回结果的统一处理
- 支持自定义的扩展API
- 支持统一请求访问网络的流程控制
请求网络无需关心是否在主线程和非UI线程,操作UI直接可在回调处理, 保留了HttpClient的编码习惯,又加入了Builder模式编程!
Novate novate = new Novate.Builder(this)
.baseUrl(baseUrl)
.build();
#更多API
novate = new Novate.Builder(this)
.addHeader(headers) //添加公共请求头
.addParameters(parameters)//公共参数
.connectTimeout(10) //连接时间 可以忽略
.addCookie(false) //是否同步cooike 默认不同步
.addCache(true) //是否缓存 默认缓存
.addCache(cache, cacheTime) //自定义缓存
.baseUrl("Url") //base URL
.addLog(true) //是否开启log
.cookieManager(new NovateCookieManager()) // 自定义cooike
.addInterceptor() // 自定义Interceptor
.addNetworkInterceptor() // 自定义NetworkInterceptor
.proxy(proxy) //代理
.client(client) //clent 默认不需要
.build();
novate.executeGet("pathUrl", parameters(k-v), new Novate.ResponseCallBack<NovateResponse<MyModel>>() {
.....
});
novate.executePost("pathUrl", parameters(k-v), new Novate.ResponseCallBack<NovateResponse>() {
.............
});
novate.body(url, Object, new BaseSubscriber() { @Override public void onError(Throwable e) {
}
@Override
public void onNext(ResponseBody responseBody) {
}
});
#FORM#
novate.form(url, new HashMap(), new BaseSubscriber() { @Override public void onError(Throwable e) {
}
@Override
public void onNext(ResponseBody responseBody) {
}
});
novate.json(url, jsonString, new BaseSubscriber<ResponseBody>() {
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ResponseBody responseBody) {
}
});
upLoadImage
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpg"), new File(you file path));
novate.upload(url, requestFile, new BaseSubscriber<ResponseBody>{
'''''''''''''''
});
upLoadFile
String path = "you File path ";
String url = "";
File file = new File(path);
// 创建 RequestBody,用于封装 请求RequestBody
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
// 添加描述
String descriptionString = "hello, 这是文件描述";
RequestBody description =
RequestBody.create(
MediaType.parse("multipart/form-data"), descriptionString);
// 执行
novate.uploadFlie(url, description, body,new BaseSubscriber<ResponseBody>(ExempleActivity.this) {.......
});
upLoadFiles
Map<String, RequestBody> fileMaps = new HashMap<>(); maps.put("key1", requestFile1); maps.put("key2", requestFile2);
novate.uploadFlies(url, fileMaps, new BaseSubscriber<ResponseBody>(Context) {
......
} );
downLoad for MaxFile
novate.download(downUrl, new DownLoadCallBack() {
''''''''''''
});
downLoad for minFile
novate.downloadMin(downUrl, new DownLoadCallBack() {
''''''''''''
});
如果默认的BaseApiService无法满足你的需求时,novate同样支持你自己的ApiService。
MyApi
public interface MyApi {
@GET("url")
Observable<MyBean> getdata(@QueryMap Map<String, String> maps);
}
Execute
MyApi myApi = novate.create(MyApi.class);
novate.call(myAPI.getdata(parameters),
new BaseSubscriber<MyBean>{
'''''''
});
}
如果你觉得此框架的业务码和错误码定的太死,其实框架已提供定制化方案,比如可以在你的项目中Assets中修改config文件:
如果想用自带的成功状态码0,不成功为非零的情况,可忽略下面的配置,无需改动。
{ "isFormat": "false", "sucessCode": [ "1", "0", "1001" ], "error": { "1001": "网络异常" } }
如果不想对结果格式化检查,请将isFormat
设置为:false
如果想修改sucessCode的成功业务码,请将你的成功的业务码加入到sucessCode
节点中。
错误码
需要对错误码进行自定义翻译,请配置相关error
节点信息,具体可配置成:
`{
"isFormat": "false",
"sucessCode": [
"1",
"0",
"1001"
],
"error": {
"1001": "网络异常",
"1002": "加入你的异常信息"
}
}
统一网络和Loading
继承Novate自带的的BaseSubscriber<T>
,复写onStart()
和onCompleted()
前者显示loading,后者结束loading.
@Override
public void onStart() {
super.onStart();
Log.v("Novate", "-->http is start");
// todo some common as show loadding and check netWork is NetworkAvailable
// if NetworkAvailable no ! must to call onCompleted
}
@Override
public void onCompleted() {
Log.v("Novate", "-->http is Complete");
// todo some common as dismiss loadding
}
版本历史: https://bintray.com/neglectedbyboss/maven/Novate
-
V1.3.0*: 修复下载API在某些机型上文件夹创建失败情况。
2017.5
。 -
V1.2.9*: 强化取消请求API.
2017.1
。 -
V1.2.8*: 修复数据被备份的安全漏洞。
2017.1
-
V1.2.7*: 优化相关下载代码。优化cookie同步时对某些网站不兼容问题,
2016.12
。 -
V1.2.6.x: 优化相关下载代码,并提交遗漏的put和delete方法,并将Http默认结果码回调到错误结果码中,并增加对请求参数的泛型支持 。
2016.12
-
V1.2.5-bata: 提供只对Response真实数据(T data)处理的功能,简化上层调用方式,但是不灵活,可选择使用,。
2016.11
-
V1.2.3: 增加对缓存功能的配置开关,可选择的对api进行缓存。
2016.11
-
V1.2.2: 解决对Response一些转换异常。
2016.11
-
V1.2.1: 增加对json的提交的支持。
2016.11
-
V1.2.0: 增加对Response数据结构格式,业务码,错误码的配置功能。提供自定义配置成功码和错误码功能。
2016.11
-
V 1.1.1 : 更新对返回结果异常的判断分发出处理
2016.10
-
V1.1.0: 增加Response异常处理和容错处理。
2016.10
-
V 1.0.2 : 增加body提交方式,增加小文件下载,增加表单方式提交功能,并新增设置是否同步cookie接口 .
2016.9
-
V1.0.1: 扩展下载接口,可以制定下载路径和文件名,包括修复下载抛异常问题。
2016.8
-
V 1.0 : 基于retrofit和Rxjava完成以泛型基础的get, put, Post delete, upLoad, downLoad功能
2016.6
Copyright 2013 Square, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.