一、Android json数据Log格式化打印
Android项目中的网络请求返回的数据一般都是json格式,而且可能很长,在log中就是长长的一大串堆在那里,还需要复制出来进行格式化处理,很麻烦。
解决方案代码如下:
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
} else {
Log.d(tag, "╚═══════════════════════════════════════════════════════════════════════════════════════");
}
}
public static void printJson(String tag, String msg, String headString) {
String message;
try {
if (msg.startsWith("{")) {
JSONObject jsonObject = new JSONObject(msg);
message = jsonObject.toString(4);//最重要的方法,就一行,返回格式化的json字符串,其中的数字4是缩进字符数
} else if (msg.startsWith("[")) {
JSONArray jsonArray = new JSONArray(msg);
message = jsonArray.toString(4);
} else {
message = msg;
}
} catch (JSONException e) {
message = msg;
}
printLine(tag, true);
message = headString + LINE_SEPARATOR + message;
String[] lines = message.split(LINE_SEPARATOR);
for (String line : lines) {
Log.d(tag, "║ " + line);
}
printLine(tag, false);
}
二、Retrofit打印请求地址和返回内容
用过retrofit的同学,肯定会很爽,因为用起来实在是方便。但是我之前在使用retrofit的时候,发现没法打印出网络请求日志,包括请求urll、返回内容等。要实现打印日志,就要用到HttpLoggingInterceptor这个类。下面给大家讲一下如何打印出这些内容。
1、导入库
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2、初始化HttpLoggingInterceptor
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
//Android json数据Log格式化打印
printJson("qqqqqq",message,"aaaaaa");
//打印retrofit日志
Log.i("RetrofitLog","retrofitBack = "+message);
}
});
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
3、配置okhttp
OkHttpClient client = new OkHttpClient.Builder()
// .cache(cache)
.addInterceptor(loggingInterceptor)
//.connectTimeout(mTimeOut, TimeUnit.SECONDS)
//.readTimeout(mTimeOut, TimeUnit.SECONDS)
//.writeTimeout(mTimeOut, TimeUnit.SECONDS)
.build();
4、截屏案例代码如下:
Retrofit打印请求地址和返回内容.png
5、配置retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(userCenter)
.client(getClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
添加统一的请求头Header
1、导入库
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
2、初始化HttpLoggingInterceptor
//设置headers
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json")
.addHeader("X-App-Id", "")
.addHeader("X-Access-Token", "UUEOTtGAnh7KM-OFlYWP0ya3PUCvtqlW")
.addHeader("X-UUID", "")
.build();
return chain.proceed(request);
}
};
3、配置okhttp
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(interceptor)
.build();
4、配置retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(userCenter)
.client(getClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
网友评论