不錯(cuò)的rxjava+retrofit文章
Retrofit + OkHttp3 自定義全局Header和Cookie管理
RxJava 與 Retrofit 結(jié)合的最佳實(shí)踐
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是針對(duì)需要真實(shí)請(qǐng)求網(wǎng)絡(luò)的請(qǐng)求的攔截器 Interceptor則會(huì)攔截所有請(qǐng)求,包括讀取緩存的請(qǐng)求