依赖
implementation 'com.orhanobut:logger:2.2.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
日志
import android.text.TextUtils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
public class LogUtil {
public static void init() {
FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
.showThreadInfo(false)//是否展示线程信息
.methodCount(0)//展示多少个方法 默认2
.methodOffset(7)//隐藏要偏移的内部方法调用 默认5
.logStrategy(null)//改变日志策略
.tag("baway")//设置统一标志
.build();
Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
}
public static void json(String json) {
if (!TextUtils.isEmpty(json)) {
Logger.json(json);
}
}
public static void json(String tag,String json) {
if (!TextUtils.isEmpty(json)) {
Logger.t(tag).json(json);
}
}
public static void d(String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.d(msg);
}
}
public static void d(String tag,String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.t(tag).d(msg);
}
}
public static void e(String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.e(msg);
}
}
public static void w(String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.w(msg);
}
}
public static void v(String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.v(msg);
}
}
public static void i(String msg) {
if (!TextUtils.isEmpty(msg)) {
Logger.i(msg);
}
}
}
OKhttp拦截器
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/*
* 请求拦截器
* */
public class OkHeaderInterceptor implements Interceptor{
private Map<String,String> headers;
public OkHeaderInterceptor(Map headers) {
if (headers!=null){
this.headers = headers;
}
}
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder builder = request.newBuilder()
.addHeader("version", "version.0")
.addHeader("platform", "android");
if (headers!=null){
Set set = headers.keySet();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
String next = iterator.next();
builder.addHeader(next,headers.get(next));
}
}
request = builder.build();
return chain.proceed(request);
}
}