首先,本文不是介绍或讲解,而是实例。
知道点进来的最想要什么,我也不写废话,直接贴代码!
需要添加的依赖
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.okhttp3:okhttp:3.1.2'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
这段才是本体
public class ServiceFactory {
static final String TAG = "ServiceFactory";
public static String baseUrl;
private Retrofit retrofit;
private static volatile ServiceFactory mInstance;
public static ServiceFactory getInstance() {
if(null == mInstance){
synchronized (ServiceFactory.class) {
if(null == mInstance) mInstance = new ServiceFactory();
}
}
return mInstance;
}
private ServiceFactory(){
retrofit = new Retrofit.Builder()
.client(createHttpClient())
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public <T> T create(Class<T> reqInterface) {
return retrofit.create(reqInterface);
}
private OkHttpClient createHttpClient() {
return new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.addInterceptor(createLoggingInterceptor())
.addInterceptor(createParamInterceptor())
.build();
}
//创建日志拦截器
private Interceptor createLoggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> {
try {
String text = URLDecoder.decode(message, "utf-8");
Log.d(TAG, text);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e(TAG, message);
}
});
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
//创建公共参数拦截器。
private Interceptor createParamInterceptor() {
return chain -> {
Request request = chain.request();
HttpUrl url = request.url(); //原url
StringBuilder urlBuilder = new StringBuilder(url.toString());
if(url.toString().contains("?")) urlBuilder.append("&");
else urlBuilder.append("?");
StringBuilder paramBuilder = new StringBuilder()
.append("¶m1=").append(param1)
.append("¶m2=").append(param1);
Log.d(TAG, "public parameter : " + paramBuilder.toString());
//url拼接公共参数后重新构建请求
Request newReq = request.newBuilder().url(urlBuilder.append(paramBuilder).toString()).build();
return chain.proceed(newReq);
};
}
}
定义一个接口
public interface CommonRequest {
@GET("......")
Observable<Login> doLogin(@QueryMap Map<String, String> map);
//这个返回的Login对应到json映射的实体类。
}
用起来!
HashMap<String, String> map = new HashMap<>();
map.put("userName", userName);
map.put("passWord", passWord);
ServiceFactory.getInstance().create(CommonRequest.class)
.doLogin(map)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Login>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Login login) {
//这个返回的bean对应到json映射的实体类。
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
以上
转载请注明出处,@via 小风风吖-详细的Retrofit2 + RxJava2封装 蟹蟹。