Retrofit 创建

不错的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则会拦截所有请求,包括读取缓存的请求

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容