retrofit 打印请求地址和参数

个人用的 retrogit +rxjava 框架

下面是对应得依赖

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    implementation 'io.reactivex:rxandroid:1.1.0'
    //okhttp3
    implementation 'com.squareup.okhttp3:okhttp:4.10.0'
    implementation "com.squareup.okhttp3:logging-interceptor:4.10.0"
/声明日志类
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//设定日志级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

//自定义OkHttpClient
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
//添加拦截器
        okHttpClient.addInterceptor(httpLoggingInterceptor);

 

在retrofit添加 .client(okHttpClient.build())

 

    public void getUrl(String url, int pageNum) {
//声明日志类
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//设定日志级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

//自定义OkHttpClient
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
//添加拦截器
        okHttpClient.addInterceptor(httpLoggingInterceptor);

        String token = SPUtils.getParam(AppConfig.getAppConfig(), "token", "").toString();
        Map hah = new HashMap<>();
        hah.put("orderNo", "");
        hah.put("orderStatus", "");
        hah.put("pageNum", pageNum);
        hah.put("pageSize", "20");
        hah.put("reasonable", "false");
        list = new ArrayList<>();
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();

        ApiService apiService = retrofit.create(ApiService.class);
        Observable homes = apiService.getAll(token, hah);

        homes.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("all_list", "onError: " + e);
                    }

                    @Override
                    public void onNext(GoodsListBean goodsListBean) {
                        int code = goodsListBean.getCode();
                        Log.d("all_list", "onNext: " + code + "==message:" + goodsListBean.getMsg().toString());
                        list = goodsListBean.getData().getList();
                        onFinish.OnFinishListener(list);
                    }
                });
    }

你可能感兴趣的:(retrofit,android,rxjava)