原文地址 :http://www.jianshu.com/p/29c2a9ac5abf
基于Retrofit2.0封装的RetrofitClient.
使用原生的Retrofit请求网络,熟悉的朋友必定了解,在某个ApiServie方法多时 Retrofit设置就显得有点累赘,今天给大家带来对Retrofit的基本封装。这次对Retrofit进阶篇,本次封装已加入RxJava,请在阅读下文前请先了解RXJAVA和本人写的Retrofit系列文章,
基本步骤:
构建Retrofit的接口service.
构建基础拦截器 Interceptor.
构建Cookie管理工具CookieManger.
构建 单列RetrofitClient客户端.
RetrofitClient的使用.
请求网络的API接口类,这里你可以增加你需要的请求接口,也可复用已经实现的几个方法。
/**
* Created by Tamic on 2016-07-08.
*/
public interface ApiService {
public static final String Base_URL = "http://ip.taobao.com/";
/**
*普通写法
*/
@GET("service/getIpInfo.php/")
Observable<ResponseBody> getData(@Query("ip") String ip);
@GET("{url}")
Observable<ResponseBody> executeGet(
@Path("url") String url,
@QueryMap Map<String, String> maps);
@POST("{url}")
Observable<ResponseBody> executePost(
@Path("url") String url,
@QueryMap Map<String, String> maps);
@Multipart
@POST("{url}")
Observable<ResponseBody> upLoadFile(
@Path("url") String url,
@Part("image\"; filename=\"image.jpg") RequestBody avatar);
@POST("{url}")
Call<ResponseBody> uploadFiles(
@Path("url") String url,
@Part("filename") String description,
@PartMap() Map<String, RequestBody> maps);
}
上面新增了几个常用的请求方法
第一个只是普通写法的列子, url ,请求头,参数都是写死的。 不建议这么做
第二,三个分别是Get 和POST请求,method Url,headers, body参数都可以动态外部传入。
四 五是单文件/图片和多文件/图片上传
用来设置基础header,这里是通过MAP键值对来构建,将heder加入到Request中。
/**
* BaseInterceptor,use set okhttp call header
* Created by Tamic on 2016-06-30.
*/
public class BaseInterceptor implements Interceptor{
private Map headers;
public BaseInterceptor(Map headers) {
this.headers = headers;
}
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request()
.newBuilder();
if (headers != null && headers.size() > 0) {
Set keys = headers.keySet();
for (String headerKey : keys) {
builder.addHeader(headerKey, headers.get(headerKey)).build();
}
}
return chain.proceed(builder.build());
}
}
用来管理cookie, 储存cookie的store这里不再重复说明,具体列子请见:
public class NovateCookieManger implements CookieJar {
private static final String TAG = "NovateCookieManger";
private static Context mContext;
private static PersistentCookieStore cookieStore;
/**
* Mandatory constructor for the NovateCookieManger
*/
public NovateCookieManger(Context context) {
mContext = context;
if (cookieStore == null) {
cookieStore = new PersistentCookieStore(mContext);
}
}
@Override
public void saveFromResponse(HttpUrl url, List cookies) {
if (cookies != null && cookies.size() > 0) {
for (Cookie item : cookies) {
cookieStore.add(url, item);
}
}
}
@Override
public List loadForRequest(HttpUrl url) {
List cookies = cookieStore.get(url);
return cookies;
}
}
今天重要的环节来了,RetrofitClient主要负责创建具体Retrofit,和调度分发请求。设置格式工厂。添加cookie同步,构建OkHttpClient,添加BaseUrl,对加密证书https我没做加入,希望读者参考我的本系列文章自行加入,因为我不喜欢升伸手党。
/**
* RetrofitClient
* Created by Tamic on 2016-06-15.
*/
public class RetrofitClient {
private static final int DEFAULT_TIMEOUT = 5;
private ApiService apiService;
private OkHttpClient okHttpClient;
public static String baseUrl = ApiService.Base_URL;
private static Context mContext;
private static RetrofitClient sNewInstance;
private static class SingletonHolder {
private static RetrofitClient INSTANCE = new RetrofitClient(
mContext);
}
public static RetrofitClient getInstance(Context context) {
if (context != null) {
Log.v("RetrofitClient", DevUtil.isDebug() + "");
mContext = context;
}
return SingletonHolder.INSTANCE;
}
public static RetrofitClient getInstance(Context context, String url) {
if (context != null) {
mContext = context;
}
sNewInstance = new RetrofitClient(context, url);
return sNewInstance;
}
private RetrofitClient(Context context) {
this(context, null);
}
private RetrofitClient(Context context, String url) {
if (TextUtils.isEmpty(url)) {
url = baseUrl;
}
okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(
new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
.cookieJar(new NovateCookieManger(context))
.addInterceptor(new BaseInterceptor(mContext))
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(url)
.build();
apiService = retrofit.create(ApiService.class);
}
public void getData(Subscriber subscriber, String ip ) {
apiService.getData(ip)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
public void get(String url, Map headers, Map parameters, Subscriber subscriber ) {
apiService.executeGet(url, headers, parameters)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
public void post(String url, Map headers, Map parameters, Subscriber subscriber ) {
apiService.executePost(url, headers, parameters)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}
RetrofitClient.getInstance(this).getData(new Subscriber() {
@Override
public void onCompleted() {
Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_LONG).show();
}
@Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, "失败!: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onNext(ResponseBody ResponseBody) {
Toast.makeText(MainActivity.this, ResponseBody.toString(), Toast.LENGTH_LONG).show();
}
}, "21.22.11.33");
代码很简洁,在用到的地方获取单列直接调用你需要的方法,在RxSubscriber回调中处理你的业务逻辑即可,无需考虑是否在主线程,其他调用方法同上。
很多时候BaseApiService无法满足需求时,Retrofit增加了扩展接口 create
来创建你的API,接着调用execute就可以和RxJava关联
//create you APiService
MyApiService service =
RetrofitClient.getInstance(MainActivity.this).create(MyApiService.class);
// execute and add observable
RetrofitClient.getInstance(MainActivity.this).execute(
service.getData("21.22.11.33"), new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onNext(IpResult responseBody) {
Toast.makeText(MainActivity.this, responseBody.toString(), Toast.LENGTH_LONG).show();
}
});}
本次封装只对retrofit进行了简单封装,很多场景和需求还是存在缺陷,这种单列模式已不符合目前流行的Builder模式,本人已开始进行下一步的封装工作,在这里提前进行下预告:
笔者已进行新的框架开发novate,估计下个月就能和大家见面,敬请继续关注!
源码 GitHub :https://github.com/NeglectedByBoss/RetrofitClient
已全部更新完成
系列导读:
Retrofit 2.0(一) 超能实践,完美支持Https传输
Retrofit2.0(二) 完美同步Cookie实现免登录
Retrofit 2.0 超能实践(三),轻松实现文件/图片上传
Retrofit 2.0 超能实践(四),完成大文件断点下载
基于Retrofit2.0 封装的超好用的RetrofitClient工具类
玩转IOC,教你徒手实现自定义的Retrofit框架
Rxjava和Retrofit 需要掌握的几个实用技巧,缓存问题和统一对有无网络处理问题