Android Retrofit2 0 查看log和JSON字符串(HttpLoggingInterceptor)
时间:2022-03-12 19:26
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); }
compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4‘ // retrofit compile ‘com.google.code.gson:gson:2.5‘ // gson compile ‘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); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!
相关推荐
- Android系统编程入门系列之界面Activity交互响应
- 新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- uni-app滚动视图容器(scroll-view)之监听上拉事件
- uniapp h5,app两端复制文本
- Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
- 诺基亚短信生成!太好玩了
- iOS 跳转App Store进行评分
- 开发一个即时通讯App
- 关闭苹果IOS app自动更新
电脑软件
本类排行
- 1关闭苹果IOS app自动更新
- 2iOS 跳转App Store进行评分
- 3诺基亚短信生成!太好玩了
- 4Android系统编程入门系列之界面Activity响应丝滑的传统动画
- 5uniapp h5,app两端复制文本
- 6uni-app滚动视图容器(scroll-view)之监听上拉事件
- 7新型横向移动工具原理分析、代码分析、优缺点以及检测方案
- 8Android系统编程入门系列之界面Activity交互响应
- 9开发一个即时通讯App
- 10【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn