不错的rxjava+retrofit文章
Retrofit + OkHttp3 自定义全局Header和Cookie管理
RxJava 与 Retrofit 结合的最佳实践
RxJava+Retrofit
扔物线
(1)module里build.gradle需要引用:
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.2'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
log拦截器
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS).setLevel(HttpLoggingInterceptor.Level.BODY);
** header 拦截器**
Interceptor headerInterceptor = new Interceptor()
{
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("**", "**")
.addHeader("**", "**")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
};
public interface UserInterface {
@GET("top250")
Observable<HttpResult<List<Subject>>> getTopMovie(@Query("start") int start, @Query("count") int count);}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://baidu.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(okHttpClient())
.build();
UserInterface uesrinterface = retrofit.create(UserInterface.class);
** NetworkInterceptor Interceptor 讲解**
如果你没有使用缓存,用哪种都一样。 NetworkInterceptor是针对需要真实请求网络的请求的拦截器 Interceptor则会拦截所有请求,包括读取缓存的请求