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 getData(@Query("catalogId")String catalogId, @Query("pnum")int pnum);
}

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 T getApiService(String url,Class cl){
Retrofit retrofit = getRetrofit(url);
return retrofit.create(cl);
}
}

    推荐阅读