Retrofit+Rxjava+okHttp日志拦截器


compile 'io.reactivex.rxjava2:rxjava:2.0.1'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'


//ok拦截日志

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
//自定义接口   存放uri串
public interface Getnet {
    //retrofit要求有一个根地址,此处我们用主机域名作为根地址,/严格要求必须有
    //http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10   你要解析的uri串
    public static String net="http://www.yulin520.com/a2a/";

}

//接口为  网络请求的方式
public interface TestService {
    //User  为uri串   转换的bean对象
      //impressApi/news/mergeList?pageSize=10&page=10为
    @GET("impressApi/news/mergeList?pageSize=10&page=10")   
        Observable getUser();
@GET( "impressApi/news/mergeList?pageSize=10")
Observable getUsermap(@QueryMap Map map);
@POST( "impressApi/news/mergeList?pageSize=10") @FormUrlEncoded
Observable postUser(@Field("page") String id);
@POST( "impressApi/news/mergeList?pageSize=10") @FormUrlEncoded
Observable postUsermap(@FieldMap Map map);
}


 //工具包   解析json串的时候用
public class Util {
    private volatile static Util util=null;
    private Util(){

    }
    public static Util getmInstance(){
        if (util==null){
            synchronized (Util.class){
                if (util==null){
                    util=new Util();
                }
            }
        }
        return util;
    }

   OkHttpClient okHttpClient = new OkHttpClient.Builder().
      addInterceptor(new MyInter()).build();
//ok拦截日志
OkHttpClient httpClient = new OkHttpClient.Builder().
        addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .build();
public Testservice getnetjson(String uri){ Retrofit retrofit = new Retrofit.Builder().baseUrl(uri)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttpClient )//添加拦截器时用
                                              
               .build();

             Testservive testservive = retrofit.create(Testservive.class);
return testservive;

//拦截器
class MyInter implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        //获取原来的body
        Request request = chain.request();
        RequestBody body = request.body();
        if (body instanceof FormBody) {
            //遍历原来的所有参数,加到新的Body里面,最后将公共参数加到新的Body
            FormBody.Builder newBuilder = new FormBody.Builder();
            for (int i = 0; i < ((FormBody) body).size(); i++) {
                String name = ((FormBody) body).name(i);
                String value = ((FormBody) body).value(i);

                //放入新的
                newBuilder.add(name, value);
            }
            //在将公共参数添加进去
            newBuilder.add("page", "20");
            FormBody newBody = newBuilder.build();
            //创建新的请求
            Request newRequest = request.newBuilder().post(newBody).build();
            Response response = chain.proceed(newRequest);
            return response;
        }

        return null;
    }
}
//解析json串代码
   Testservive testservive = Util.getnetjson(Getnet.net);
   Observable user1 = testservive.getUser(map);
      user1.subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer() {
            @Override
            public void accept(User user) throws Exception {
                List data = user.getData();
               
            }
        });

你可能感兴趣的:(Retrofit+Rxjava+okHttp日志拦截器)