android網(wǎng)絡(luò)日志打?。ㄓ涗洠?/h2>

OkHttp攔截類(繼承Interceptor?)

public class OkHttpLoggerInterceptor implements Interceptor {

????//定義字符類型為UTF-8

? ? private static final Charset UTF8 = Charset.forName("UTF-8");

? ? @Override

? ? public Responseintercept(Chain chain)throws IOException {

????????//獲取請求

? ? ? ? Request request = chain.request();

? ? ? ? {

????????????//獲取請求體

? ? ? ? ? ? RequestBody requestBody = request.body();

? ? ? ? ? ? //獲取請求頭

? ? ? ? ? ? Headers headers = request.headers();

? ? ? ? ? ? //判斷請求體是否為空

? ? ? ? ? ? boolean hasRequestBody = requestBody !=null;

? ? ? ? ? ? //開始打印請求體日志

? ? ? ? ? ? LogUtil.e("┎━━━━━ request start ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? ? ? LogUtil.e("┃" + request.url() +"? " + request.method());

? ? ? ? ? ? if (hasRequestBody) {

????????????????//請求體不為空

? ? ? ? ? ? ? ? if (requestBody.contentType() !=null) {

????????????????//請求體格式類型不為空則打?。ㄓ脕砼袛嗾埱篌w字符類型還是咋樣,不太清楚用途,自行看源碼)

? ? ? ? ? ? ? ? ? ? LogUtil.e("┃contentType: " + requestBody.contentType());

? ? ? ? ? ? ? ? }

if (requestBody.contentLength() != -1) {

????????????????????//請求體長度不為-1則打印

? ? ? ? ? ? ? ? ? ? LogUtil.e("┃contentLength: " + requestBody.contentLength());

? ? ? ? ? ? ? ? }

}

for (int i =0; i < headers.size(); i++) {

????????????????//打印請求頭

? ? ? ? ? ? ? ? String name = headers.name(i);

? ? ? ? ? ? ? ? if (!"Content-Type".equalsIgnoreCase(name) && !"Content-

????????????????Length".equalsIgnoreCase(name))

????????????????LogUtil.e("┃" + headers.name(i) +":" + headers.value(i));

? ? ? ? ? ? }

if (!hasRequestBody) {

????????????????//請求體為空打印結(jié)束語

? ? ? ? ? ? ? ? LogUtil.e("┖━━━━━ request end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? ? ? }else if (bodyEncoded(request.headers())) {

????????????????LogUtil.e("┖━━━━━ request end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" +"?

????????????????(encoded body omitted)");

? ? ? ? ? ? }else {

????????????????//打印請求體內(nèi)容

? ? ? ? ? ? ? ? Buffer buffer =new Buffer();

? ? ? ? ? ? ? ? requestBody.writeTo(buffer);

? ? ? ? ? ? ? ? MediaType contentType = requestBody.contentType();

? ? ? ? ? ? ? ? String bufferString = buffer.readString(UTF8);

? ? ? ? ? ? ? ? if (contentType !=null &&"json".equals(contentType.subtype())) {

????????????????????//是json格式打印

? ? ? ? ? ? ? ? ? ? LogUtil.eJson(bufferString);

? ? ? ? ? ? ? ? }

????????????????LogUtil.e(bufferString);

? ? ? ? ? ? ? ? LogUtil.e("┖━━━━━ request end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ " +?

????????????????requestBody.contentLength());

? ? ? ? ? ? }

}

{

????????????long startNs = System.nanoTime();

? ? ? ? ? ? //獲取響應(yīng)

? ? ? ? ? ? Response response = chain.proceed(request);

? ? ? ? ? ? //獲取響應(yīng)時間

? ? ? ? ? ? long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

? ? ? ? ? ? //獲取響應(yīng)體

? ? ? ? ? ? ResponseBody responseBody = response.body();

? ? ? ? ? ? long contentLength = responseBody.contentLength();

? ? ? ? ? ? //開始打印響應(yīng)體日志

? ? ? ? ? ? LogUtil.e("┎━━━━━ response start ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? ? ? LogUtil.e("┃" + response.request().url() +"? " + response.code() +"? " +?

????????????response.message() +" (" + tookMs +"ms" +"" +')');

? ? ? ? ? ? //獲取響應(yīng)頭

? ? ? ? ? ? Headers headers = response.headers();

? ? ? ? ? ? for (int i =0; i < headers.size(); i++) {

????????????//全部打印出來

? ? ? ? ? ? ? ? LogUtil.e("┃" + headers.name(i) +":" + headers.value(i));

? ? ? ? ? ? }

if (!HttpHeaders.hasBody(response)) {

????????????????//響應(yīng)體為空打印結(jié)束語

? ? ? ? ? ? ? ? LogUtil.e("┖━━━━━ response end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? ? ? }else if (bodyEncoded(response.headers())) {

????????????????LogUtil.e("┖━━━━━ response end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━?

????????????????(encoded body omitted)");

? ? ? ? ? ? }else {

????????????????//響應(yīng)體只能獲取一次,所以不能用string()

????????????????//String source = responseBody.string().trim();

????????????????//獲取響應(yīng)體資源(用source()方法獲取響應(yīng)體內(nèi)容不會影響響應(yīng)體的傳輸)

? ? ? ? ? ? ? ? BufferedSource source = responseBody.source();

? ? ? ? ? ? ? ? source.request(Long.MAX_VALUE); // Buffer the entire body.

? ? ? ? ? ? ? ? Buffer buffer = source.buffer();

? ? ? ? ? ? ? ? if (contentLength !=0) {

????????????????????MediaType contentType = responseBody.contentType();

? ? ? ? ? ? ? ? ? ? String bufferString = buffer.clone().readString(UTF8);

? ? ? ? ? ? ? ? ? ? if (contentType !=null &&"json".equals(contentType.subtype())) {

????????????????????????//是json格式

? ? ? ? ? ? ? ? ? ? ? ? LogUtil.eJson(bufferString);

? ? ? ? ? ? ? ? ? ? }

????????????????????????LogUtil.e("┃" + bufferString);

? ? ? ? ? ? ? ? }

????????????????????????LogUtil.e("┖━━━━━ response end ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ "?

????????????????????????+ buffer.size() +"-byte body)");

? ? ? ? ? ? }

????????????????????????return response;

? ? ? ? }

}

????/*請求頭編碼格式???不是很清楚用途*/

? ? private boolean bodyEncoded(Headers headers) {

????????String contentEncoding = headers.get("Content-Encoding");

? ? ? ? return contentEncoding !=null && !contentEncoding.equalsIgnoreCase("identity");

? ? }

}

日志打印類

public final class LogUtil {

private static final boolean isDebug =true;

? ? private final String RAILA ="┎┖━┃";

? ? private static final String LEFT_TOP_RAILA ="┎";

? ? private static final String LEFT_BOTTOM_RAILA ="┖";

? ? private static final String LEFT_RAILA ="┃";

? ? /*正常日志*/

? ? public static void e(String logs) {

if (isDebug) {

????Log.e(getRandom(), logs);

? ? ? ? }

}

/*打印有調(diào)用類的日志*/

? ? public static void eSuper(String logs) {

? ? ? ? if (isDebug) {

????????????StackTraceElement[] elements = Thread.currentThread().getStackTrace();

? ? ? ? ? ? for (int i =0; i < elements.length; i++) {

????????????????if (elements[i].getClassName().equals(LogUtil.class.getName())) {

Log.e(getRandom(), LEFT_TOP_RAILA +"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

//? ? ? ? ? ? ? ? ? ? Log.e(getRandom(), LEFT_RAILA + elements[i + 1].getFileName());

? ? ? ? ? ? ? ? ? ? Log.e(getRandom(), LEFT_RAILA + elements[i] +"");

? ? ? ? ? ? ? ? ? ? Log.e(getRandom(), LEFT_RAILA + elements[i +1] +"");

? ? ? ? ? ? ? ? }

}

Log.e(getRandom(), "┠━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? ? ? Log.e(getRandom(), LEFT_RAILA + logs);

? ? ? ? ? ? Log.e(getRandom(), LEFT_BOTTOM_RAILA +"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

? ? ? ? }

}

/*正常日志*/

? ? public static void eNormal(String tag, String logs) {

if (isDebug) {

Log.e(tag, logs);

? ? ? ? }

}

/*打印json格式的日志*/

? ? public static void eJson(String logs) {

if (isDebug) {

Log.e(getRandom(), LEFT_RAILA + JsonFormatter.jsonFormat(logs));

? ? ? ? }

}

/*有調(diào)用類的tag*/

? ? private static StringgetTAG() {

StackTraceElement[] elements = Thread.currentThread().getStackTrace();

? ? ? ? for (int i =0; i < elements.length; i++) {

if (elements[i].getClassName().equals(LogUtil.class.getName())) {

String name = elements[i +2].getFileName() +getRandom();

? ? ? ? ? ? ? ? return name;

? ? ? ? ? ? }

}

return null;

? ? }

/*獲取一萬以內(nèi)的隨機數(shù)*/

? ? private static StringgetRandom() {

int random = (int) ((Math.random() *9 +1) *10000);

? ? ? ? return random +"";

? ? }

}

json打印格式父類

public class JsonFormatter {

static final JsonFormatterformatter =findJsonFormatter();

? ? public static StringjsonFormat(String logs) {

try {

return formatter.format(logs);

? ? ? ? }catch (Exception e) {

return "";

? ? ? ? }

}

Stringformat(String logs) {

return "";

? ? }

private static JsonFormatterfindJsonFormatter() {

JsonFormatter gsonFormatter = GsonFormatter.buildIfSupported();

? ? ? ? if (gsonFormatter !=null) {

return gsonFormatter;

? ? ? ? }

JsonFormatter jsonFormatter = OrgJsonFormatter.buildIfSupported();

? ? ? ? if (jsonFormatter !=null) {

return jsonFormatter;

? ? ? ? }

return new JsonFormatter();

? ? }

}

json打印格式子類(Gson)

public class GsonFormatterextends JsonFormatter {

private final GsonGSON =new GsonBuilder().setPrettyPrinting().create();

? ? private final JsonParserPARSER =new JsonParser();

? ? @Override

? ? Stringformat(String logs) {

return GSON.toJson(PARSER.parse(logs));

? ? }

protected static GsonFormatterbuildIfSupported(){

try {

Class.forName("com.google.gson.Gson");

? ? ? ? ? ? return new GsonFormatter();

? ? ? ? }catch (ClassNotFoundException e) {

return null;

? ? ? ? }

}

}

json打印格式子類(JSONObject)

public class OrgJsonFormatterextends JsonFormatter {

@Override

? ? Stringformat(String logs) {

try {

return new JSONObject(logs).toString(4);

? ? ? ? }catch (JSONException e) {

return "";

? ? ? ? }

}

protected static OrgJsonFormatterbuildIfSupported() {

try {

Class.forName("org.json.JSONObject");

? ? ? ? ? ? return new OrgJsonFormatter();

? ? ? ? }catch (ClassNotFoundException e) {

return null;

? ? ? ? }

}

}

若用其他json庫同上處理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,153評論 0 2
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,811評論 18 399
  • 自己備忘,隨便寫 android網(wǎng)絡(luò)框架源碼解析及對比 android常用網(wǎng)絡(luò)框架對比 Volley: 特點 基于...
    幻海流心閱讀 1,699評論 0 4
  • 現(xiàn)在是3:50分,我眼皮打架,想睡覺! 還有一節(jié)課的時間,我該把今天的小任務(wù)完成,500字就可以!只是腦子有點混沌...
    午后好時光閱讀 596評論 0 10

友情鏈接更多精彩內(nèi)容