基于Retrofit2.0封装的RetrofitClient.
-
避免重复创建Retrofit实列
-
调用方便简洁
-
无需重复设置属性的步骤
-
可固定配置 Host,也可动态配置Url,请求头,参数等
使用原生的Retrofit请求网络,熟悉的朋友必定了解,在某个ApiServie方法多时 Retrofit设置就显得有点累赘,今天给大家带来对Retrofit的基本封装。这次对Retrofit进阶篇,本次封装已加入RxJava,请在阅读下文前请先了解RXJAVA和本人写的Retrofit系列文章
基本步骤:
-
构建Retrofit的接口service.
-
构建基础拦截器 Interceptor.
-
构建Cookie管理工具CookieManger.
-
构建 单列RetrofitClient客户端.
-
RetrofitClient的使用.
1
ApiService
请求网络的API接口类,这里你可以增加你需要的请求接口,也可复用已经实现的几个方法。
go
/**
* 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);
@Headers("{headers}")
@GET("{url}")
Observable<ResponseBody> executeGet(
@Path("url") String url,
@Path("headers") Map<String, String> headers,
@QueryMap Map<String, String> maps);
@Headers("{headers}")
@POST("{url}")
Observable<ResponseBody> executePost(
@Path("url") String url,
@QueryMap Map<String, String> maps);
@Multipart
@Headers("{headers}")
@POST("{url}")
Observable<ResponseBody> upLoadFile(
@Path("url") String url,
@Path("headers") Map<String, String> headers,
@Part("image\\\\"; filename=\\\"image.jpg") RequestBody avatar);
@Headers("{headers}")
@POST("{url}")
Call<ResponseBody> uploadFiles(
@Path("url") String url,
@Path("headers") Map<String, String> headers,
@Part("filename") String description,
@PartMap() Map<String, RequestBody> maps);
}
上面新增了几个常用的请求方法
-
第一个只是普通写法的列子, url ,请求头,参数都是写死的。 不建议这么
-
第二,三个分别是Get 和POST请求,method Url,headers, body参数都可以动态外部传入。
-
第四 五是单文件/图片和多文件/图片上传
2
构建基础拦截器
用来设置基础header,这里是通过MAP键值对来构建,将heder加入到Request中。
go
/**
* BaseInterceptor,use set okhttp call header
* Created by Tamic on 2016-06-30.
*/
public class BaseInterceptor implements Interceptor{
private Map<String, String> headers;
public BaseInterceptor(Map<String, String> 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<String> keys = headers.keySet();
for (String headerKey : keys) {
builder.addHeader(headerKey, headers.get(headerKey)).build();
}
}
return chain.proceed(builder.build());
}
}
3
Cookie管理者
用来管理cookie, 储存cookie的store这里不再重复说明.
go
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<Cookie> cookies) {
if (cookies != null && cookies.size() > 0) {
for (Cookie item : cookies) {
cookieStore.add(url, item);
}
}
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url);
return cookies;
}
}
4
构建RetrofitClient
重要的环节来了,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<ResponseBody> 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<ResponseBody> 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<ResponseBody> subscriber) {
apiService.executePost(url, headers, parameters)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}
5
RetrofitClient
RetrofitClient初始化调用
01
RetrofitClient.getInstance(this).getData(new Subscriber<ResponseBody>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
// do something
}
@Override
public void onNext(ResponseBody ResponseBody) {
// do something
}
}, " xxxxx");
代码很简洁,在用到的地方获取单列直接调用你需要的方法,在RxSubscriber回调中处理你的业务逻辑即可,无需考虑是否在主线程,其他调用方法同上。
5
总结
本次封装只对retrofit进行了简单封装,很多场景和需求还是存在缺陷,这种单列模式已不符合目前流行的Builder模式,本人已开始进行下一步的封装工作,在这里提前进行下预告:已进行新的框架开发novate,估计下个月就能和大家见面,敬请继续关注!