package com.example.gjl.day09_mvp_retrofit_rxjava.http;
import com.google.gson.Gson;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Retrofit的工具类
*/
public class RetrofitUtil {
//单利模式
private static RetrofitUtil retrofitUtil;
private final Retrofit retrofit;
public static RetrofitUtil getInstance() {
if (retrofitUtil == null) {
retrofitUtil = new RetrofitUtil();
}
return retrofitUtil;
}
//初始化Retrofit
public RetrofitUtil() {
retrofit = new Retrofit.Builder()
.baseUrl(HttpConfig.base_url)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
}
//指定服务
public <T> T createRequest(Class<T> clz) {
T t = retrofit.create(clz);
return t;
}
}
Retrofit的工具类
最新推荐文章于 2020-07-28 17:18:39 发布