基于Retrofit2.0封装的RetrofitClient.
使用原生的Retrofit请求网络,熟悉的朋友必定了解,在某个ApiServie方法多时 Retrofit设置就显得有点累赘,今天给大家带来对Retrofit的基本封装。这次对Retrofit进阶篇,本次封装已加入RxJava,请在阅读下文前请先了解RXJAVA和本人写的Retrofit系列文章,
Retrofit 2.0
超能实践,完美支持Https传输
Retrofit2.0
完美同步Cookie实现免登录
Retrofit 2.0 超能实践,轻松实现多文件/图片上传
基本步骤:
构建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 getData(@Query("ip") String ip);
@GET("{url}")
Observable executeGet(
@Path("url") String url,
@QueryMap Map<String, String> maps);
@POST("{url}")
Observable executePost(
@Path("url") String url,
@FieldMap Map<String, String> maps);
@Multipart
@POST("{url}")
Observable upLoadFile(
@Path("url") String url,
@Part("image\\"; filename=\\"image.jpg") RequestBody avatar);
@POST("{url}")
Call uploadFiles(
@Url("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);
}
}
细心的朋友已经发现上面代码 在指定生产线程和消费线程的时候,步骤有点麻烦,每个api都得进行指定线程,那么可以利用rxJava的转换器写一个Transformer
Observable.Transformer schedulersTransformer() {
return new Observable.Transformer() {
@Override
public Object call(Object observable) {
return ((Observable) observable).subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
};
}
那么api可以这样优化了:
public Subscription getData(Subscriber subscriber, String ip) {
return apiService.getData(ip)
.compose(schedulersTransformer())
.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();
}
});}
Subscription subscription = RetrofitClient.getInstance(MainActivity.this)
.createBaseApi()
.getData(new BaseSubscriber(MainActivity.this) {
@Override
public void onError(ResponeThrowable e) {
Log.e("Lyk", e.code + " " + e.message);
Toast.makeText(MainActivity.this, e.message, Toast.LENGTH_LONG).show();
}
@Override
public void onNext(IpResult responseBody) {
Toast.makeText(MainActivity.this, responseBody.toString(), Toast.LENGTH_LONG).show();
}
}, "21.22.11.33");
subscription.unsubscribe();
Rxjava结合Retrofit,如何优雅的取消请求!
优雅的取消请看:http://www.jianshu.com/p/d62962243c33