Retrofit+RxJava封装
public class Api {
public static final String HOST = "http://api.svipmovie.com/front/";
}
【Retrofit+RxJava封装】public interface ApiService {
//拼接请求地址
@GET("columns/getVideoList.do")
Observable
}
public class RetrofitUtil {
//单例模式
private static RetrofitUtil retrofitUtil;
private static Retrofit retrofit;
public RetrofitUtil() {
}
public static RetrofitUtil getInstance(){
if (retrofitUtil==null){
synchronized (RetrofitUtil.class){
if (retrofitUtil==null){
retrofitUtil = new RetrofitUtil();
}
}
}
return retrofitUtil;
}
//封装Retrofit
private Retrofit getRetrofit(String url){
//创建拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.e("Error", message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//创建OkHttpClicent对象
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor)
.connectTimeout(5000, TimeUnit.SECONDS)
.build();
//创建retrofit对象,封装OkHhttp , Gson , RxJava
if (retrofit==null){
retrofit = new Retrofit.Builder().baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
return retrofit;
}
public
Retrofit retrofit = getRetrofit(url);
return retrofit.create(cl);
}
}
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- python自定义封装带颜色的logging模块
- jQuery插件
- 使用Promise对微信小程序wx.request请求方法进行封装
- JavaScript|vue 基于axios封装request接口请求——request.js文件
- vue.js|vue中使用axios封装成request使用
- 第一个程序——helloworld
- Objective-c
- Swift学习笔记(三)Alamofire二次封装