retrofit +rxjava简单封装 日志拦截器 应用拦截器

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'com.google.code.gson:gson:2.6.2'

compile 'io.reactivex:rxandroid:1.2.1'

compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'


ApiService

import rx.Observable;
 
public interface ApiService {
https://www.zhaoapi.cn/quarter/getUserVideos?source=android&appVersion=101&uid=5&page=6
    @GET("user/login")
    Observable<Bean>  getTop(@QueryMap Map<String,String> map);
}

RetrofitUtils
public class RetrofitUtils {
    private static volatile  RetrofitUtils  instance;
    public static  RetrofitUtils  getInstance(){
        if(instance==null){
            synchronized (RetrofitUtils.class){
                if(instance==null){
                    instance=new RetrofitUtils();
                }
            }
        }
        return  instance;
    }


   

    public Retrofit getNews(){
   String baseUrl= "https://www.zhaoapi.cn/";

   HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
   interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
   OkHttpClient okHttpClient=new OkHttpClient.Builder()
        //  .addInterceptor(new CommonParamsinterceptor() )
        .addInterceptor(interceptor)
        .build();

Retrofit retrofit=new Retrofit.Builder()

.baseUrl(baseUrl)

.client(okHttpClient)

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.build();

return retrofit;

}

}

///

//CommonParamsinterceptor
public class CommonParamsinterceptor  implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        //得到原始的请求对象
        Request request=chain.request();

        //得到请求方式
        String method=request.method();

        if("GET".equals(method)){
            //去除原始的URL
            String oldurl=request.url().toString();

            //拼接公共参数
           // String newurl=oldurl+"source=android&appVersion=101";

            String[] f=oldurl.split("\\?");

            String s=f[0]+"?source=android&appVersion=101&"+f[1];

            Log.d("fff",s.toString());
            //构建新的request
            request=new Request.Builder()
                    .url(s)
                    .build();

        }else if("POST".equals(method)){
            //取出旧的参数和URL
            FormBody body=(FormBody)request.body();

            String oldUrl=request.url().toString();

            //构建新的FromBody
            FormBody.Builder newFormBody = new FormBody.Builder();

            for (int i = 0; i < body.size();i++){

                String key = body.name(i);//keywors
                String value = body.value(i);//value

                newFormBody.add(key,value);
            }
            //公共参数
            newFormBody.add("source","android");
            newFormBody.add("appVersion","101");

            //新构建的reqeust
            request = new Request.Builder()
                    .url(oldUrl)
                    .post(newFormBody.build())
                    .build();
        }
        return chain.proceed(request);
    }
}

//

Map<String,String> map=new HashMap<>();
map.put("mobile","13269671778");
map.put("password","1234567");

retrofitUtils = RetrofitUtils.getInstance();

retrofitUtils.getNews()
        .create(ApiService.class)
        .getTop(map)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Subscriber<Bean>() {
            @Override
            public void onCompleted() {
            }
            @Override
            public void onError(Throwable e) {
                Log.d("mmm",e.getMessage());
            }
            @Override
            public void onNext(Bean bean) {
                Log.d("xxx",bean.getMsg().toString());
            }
        });




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值