From b3a51f064c4dfb27f54cd9526803338d2e8dc296 Mon Sep 17 00:00:00 2001
From: Runt <qingingrunt2010@qq.com>
Date: Sat, 13 Aug 2022 16:26:49 +0000
Subject: [PATCH] 添加注释,部分优化

---
 app/src/main/java/com/runt/open/mvvm/retrofit/Interceptor/HttpLoggingInterceptor.java |   50 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/app/src/main/java/com/runt/open/mvvm/retrofit/Interceptor/HttpLoggingInterceptor.java b/app/src/main/java/com/runt/open/mvvm/retrofit/Interceptor/HttpLoggingInterceptor.java
index b0d62f4..556ec33 100644
--- a/app/src/main/java/com/runt/open/mvvm/retrofit/Interceptor/HttpLoggingInterceptor.java
+++ b/app/src/main/java/com/runt/open/mvvm/retrofit/Interceptor/HttpLoggingInterceptor.java
@@ -1,13 +1,18 @@
 package com.runt.open.mvvm.retrofit.Interceptor;
 
 import android.util.Log;
-
+import com.google.gson.Gson;
 import com.runt.open.mvvm.MyApplication;
+import com.runt.open.mvvm.data.PhoneDevice;
 import com.runt.open.mvvm.retrofit.net.NetWorkCost;
 import com.runt.open.mvvm.retrofit.net.NetWorkListenear;
 import com.runt.open.mvvm.retrofit.utils.HttpPrintUtils;
+import com.runt.open.mvvm.ui.login.UserBean;
 import com.runt.open.mvvm.util.DeviceUtil;
-
+import okhttp3.*;
+import okhttp3.internal.http.HttpHeaders;
+import okio.Buffer;
+import okio.BufferedSource;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -20,25 +25,13 @@
 import java.util.Date;
 import java.util.HashMap;
 
-import okhttp3.FormBody;
-import okhttp3.Headers;
-import okhttp3.MediaType;
-import okhttp3.MultipartBody;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import okhttp3.internal.http.HttpHeaders;
-import okio.Buffer;
-import okio.BufferedSource;
-
 /**
  * My father is Object, ites purpose of     log打印
  *
  * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-10-21.
  */
 
-public class HttpLoggingInterceptor extends EncryptInterceptor {
+public class HttpLoggingInterceptor extends AddHeadersInterceptor {
 
     final String TAG = "HttpLogging";
 
@@ -60,15 +53,18 @@
             Log.d(TAG, "hashcode:" + hashCode);
         }
         Request.Builder requestBuild = requestTemp.newBuilder()
+                .addHeader("device", new Gson().toJson(PhoneDevice.getDevice()))
                 .addHeader("appVersion", DeviceUtil.getAppVersionName(MyApplication.getApplication()))
                 .addHeader("os", DeviceUtil.isHarmonyOS()? "harmony" : "android");
+        if(UserBean.getUser() != null){
+            requestBuild.addHeader("token",UserBean.getUser().getToken());
+        }
         Request request = requestBuild.build().newBuilder().build();
         ArrayList<String> logArrays = new ArrayList<>();
         Response response = null;
         try {
             logArrays.addAll(getRequestLog(request));
             int position = logArrays.size() +2;
-            //request = encryptRequest(request);//加密
             response = chain.proceed(request);
             logArrays.addAll(getResponseLog(response));
             NetWorkCost netWorkCost = NetWorkListenear.workCostMap.get(hashCode);
@@ -112,6 +108,11 @@
         return response;
     }
 
+    /**
+     * 请求数据信息
+     * @param request
+     * @return
+     */
     private ArrayList<String> getRequestLog(Request request) throws IOException, JSONException {
         RequestBody requestBody = request.body();
         ArrayList<String> logArrays = new ArrayList<>();
@@ -178,7 +179,13 @@
 
     }
 
-
+    /**
+     * 返回的数据信息
+     * @param response
+     * @return
+     * @throws IOException
+     * @throws JSONException
+     */
     private ArrayList<String> getResponseLog(Response response) throws IOException, JSONException {
         ArrayList<String> logArrays = new ArrayList<>();
         ResponseBody responseBody = response.body();
@@ -205,7 +212,14 @@
             if (isPlaintext(buffer)) {
                 logArrays.add("---------->RESPONSE BODY<----------");
                 if (contentLength != 0) {
-                    logArrays.add(new JSONObject(buffer.clone().readString((charset))).toString(4));
+                    String str = buffer.clone().readString(charset);
+                    if(str.trim().indexOf("{") == 0) {
+                        logArrays.add(new JSONObject(str).toString(4));
+                    }else if(str.trim().indexOf("[") == 0) {
+                        logArrays.add(new JSONArray(str).toString(4));
+                    }else{
+                        logArrays.add(str);
+                    }
                 }
 
                 logArrays.add("<-- END HTTP (" + buffer.size() + "-byte body)");

--
Gitblit v1.9.1