Android Retrofit2.0 查看log和JSON字符串(HttpLoggingInterceptor)
来源:互联网 发布:数据库安全测试 编辑:程序博客网 时间:2024/05/16 17:12
Retrofit2.0 怎么打印请求到的json字符串和查看log呢?
关键类:HttpLoggingInterceptor
关键代码:
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(HttpLoggingInterceptor.Level.BODY);httpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
详细代码:
public static Retrofit initRetrofit(){ OkHttpClient httpClient = new OkHttpClient(); if (BuildConfig.DEBUG) { HttpLoggingInterceptor logging = new HttpLoggingInterceptor();logging.setLevel(HttpLoggingInterceptor.Level.BODY);httpClient = new OkHttpClient.Builder().addInterceptor(logging).build(); } Gson gson = new GsonBuilder() .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .create();//使用 gson coverter,统一日期请求格式 return new Retrofit.Builder() .baseUrl(BaseUtil.getGlivecApiUrl()) .addConverterFactory(GsonConverterFactory.create(gson)) .client(httpClient) .build(); } /** * 创建 RetrofitManage 服务 * * @return ApiService */ public static ApiService createApiService() { return initRetrofit().create(ApiService.class); }
.client(httpClient)属性切莫忘记配置。
retrofit:2.0.0-beta4的依赖:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' // retrofitcompile 'com.google.code.gson:gson:2.5' // gsoncompile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'compile 'com.squareup.okhttp3:okhttp:3.1.2'
也可以这么写:
public class RxService { private static final String BASETESTURL = "http://apis.baidu.com/showapi_open_bus/"; private static OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor (new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build(); private static Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASETESTURL) .client(okHttpClient) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); private RxService() { //construct } public static <T> T createApi(Class<T> clazz) { return retrofit.create(clazz); }}
3 0
- Android Retrofit2.0 查看log和JSON字符串(HttpLoggingInterceptor)
- Android Retrofit2.0 查看log和JSON字符串(HttpLoggingInterceptor)
- Retrofit2.0不进行Json解析,直接查看返回String
- Retrofit2之post 提交json字符串
- 【Android实战】----Android Retrofit2.1.0直接发送Json字符串到服务器
- Android 网络框架 HttpLoggingInterceptor优化
- Retrofit2.0 或OKHttp 日志log
- android之Retrofit2.0
- android查看kernel log
- Android-LOG错误查看
- Android modem log查看
- Android modem log查看
- android查看kernel log
- android查看kernel log
- 【Android高级】RxJava2.0和Retrofit2.0的使用探究
- Android使用Retrofit2.0和RxJava2.0处理网络请求
- Android框架学习之Retrofit(二)RxJava和Retrofit2.0的结合使用
- Android 网络框架 Retrofit2.0介绍、使用和封装
- oracle arvhivelog、noarchivelog与mount的关系
- ffmpeg 编译
- 图片转字符画
- Android属性动画ValueAnimator源码简单分析
- java内存回收相关
- Android Retrofit2.0 查看log和JSON字符串(HttpLoggingInterceptor)
- 104岁的杨绛先生送给年轻人的9句话
- Java基础回顾 : 文件类中的一些常用的方法
- hdu2222,AC自动机,trie图
- 软件工程(二十一)
- [Android]Android通用流行框架大全20160528
- 二分查找算法的原理及一个实用小技巧
- Android Retrofit 2.0自定义Converter(JSONObject Converter)
- struts2第一天基础配置 action package result 全局result redirect chain redirectAction