retrofit

public class RetrofitUtil {
private static RetrofitUtil retrofitUtil = null;
private static Retrofit mRetrofit;
private RetrofitUtil(){
mRetrofit= new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
//單例模式
public static RetrofitUtil getInstance(){
if (retrofitUtil == null) {
synchronized (RetrofitUtil.class){
if (retrofitUtil == null) {
retrofitUtil = new RetrofitUtil();
}
}
}
return retrofitUtil;
}

//獲取上面的retrofit直接添加到動態代理中
public <T> T createService(Class<T> service){
    T t = mRetrofit.create(service);
    return t;
}

}

下邊的這個是公共的方法

public interface HttpDataListener {
void onDataSuccess(T data);
void onFailer(String msg);
}

Apiservice
public interface ApiService {
@GET(Api.SHOW_URL)
Observable gethomeBean();
@GET(Api.BANN_URL)
Observable bannerBean();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章