retrofit 如何查看日志

retrofit 查看日志

近期在学习 retrofit+rxJava 这个框架的时候,下面是我学习的网站:

http://gank.io/post/56e80c2c677659311bed9841?from=timeline&isappinstalled=0&nsukey=g1D1Y6PMp3BW%2B0%2F%2Butx4StSJxcUCTm4%2BN8T7LnPNCCeQEY1lzm6oKvXdbrlAD4E9T%2FB1quV75jJB7H9zjcRxTQ%3D%3D

发现我无法查看我的网络请求和返回结果,因为有时候需要查看访问的网络请求和返回的response的结果,是否获取到json 的数据,还有json 的解析是否正确。尽管 retrofit 已经封装好解析json ,但是还是需要查看返回结果的!

  • 前提:gradle 里面需要添加相应的依赖,当然也可以下载jar 包,到对应的maven 库里面下载就好了
    compile 'com.squareup.retrofit2:retrofit:2.0.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
  • 下面就是查看日志的方法:

这里是初始化retrofit的地方

// 查看网络request 和 response 的具体的值
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);//设置查看日志的等级

OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();

okHttpBuilder.interceptors().add(logging);// 这句话是重点

//可以自定义,可以使用okHttp 默认的timeout 10s
okHttpBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS); 

retrofitBuilder = new Retrofit.Builder()
    .client(okHttpBuilder.build())
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .baseUrl(BASE_URL);

下面是调用的地方:

/** * 对 RxJava 不进行封装的原因,是可以控制事件所在的线程,这是 RxJava 的一个核心功能 * 订阅者的线程是 Schedulers.io(),观察者的线程 AndroidSchedulers.mainThread(),尽管这部分代码多数情况是相同 */
    private void doWrapperRetrofit(){
        MovieService service = RemoteApiHelper.getInstance().build().createService(MovieService.class);
        service.getRankMovieByRx(0,2)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MovieEntity>() {
                    @Override
                    public void onCompleted() { }

                    @Override
                    public void onError(Throwable e) {}

                    @Override
                    public void onNext(MovieEntity movieEntity) {}
                });
    }

返回的信息:

retrofit 如何查看日志_第1张图片

  • 日志等级的划分:

NONE, BASIC, HEADERS, BODY.
下面是源代码里面的各等级下面显示的内容

public enum Level {
    /** No logs. */
    NONE,
    /** * Logs request and response lines. * * <p>Example: * <pre>{@code * --> POST /greeting http/1.1 (3-byte body) * * <-- 200 OK (22ms, 6-byte body) * }</pre> */
    BASIC,
    /** * Logs request and response lines and their respective headers. * * <p>Example: * <pre>{@code * --> POST /greeting http/1.1 * Host: example.com * Content-Type: plain/text * Content-Length: 3 * --> END POST * * <-- 200 OK (22ms) * Content-Type: plain/text * Content-Length: 6 * <-- END HTTP * }</pre> */
    HEADERS,
    /** * Logs request and response lines and their respective headers and bodies (if present). * * <p>Example: * <pre>{@code * --> POST /greeting http/1.1 * Host: example.com * Content-Type: plain/text * Content-Length: 3 * * Hi? * --> END GET * * <-- 200 OK (22ms) * Content-Type: plain/text * Content-Length: 6 * * Hello! * <-- END HTTP * }</pre> */
    BODY
  }
  • * *
    jar 包路径 :http://download.csdn.net/download/android_freshman/9472491

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