Runt
2025-08-29 f2a3965a672fca33831d361872874fec104846d5
libmvi/src/main/java/com/runt/open/mvi/retrofit/Interceptor/HttpLoggingInterceptor.java
@@ -2,13 +2,10 @@
import android.util.Log;
import com.google.gson.Gson;
import com.runt.open.mvi.OpenApplication;
import com.runt.open.mvi.data.PhoneDevice;
import com.runt.open.mvi.retrofit.net.NetWorkCost;
import com.runt.open.mvi.retrofit.net.NetWorkListener;
import com.runt.open.mvi.retrofit.utils.HttpPrintUtils;
import com.runt.open.mvi.utils.DeviceUtil;
import com.runt.open.mvi.retrofit.utils.RSAUtils;
import org.json.JSONArray;
import org.json.JSONException;
@@ -40,31 +37,17 @@
 * @purpose Created by Runt (qingingrunt2010@qq.com) on 2020-10-21.
 */
public class HttpLoggingInterceptor extends AddHeadersInterceptor {
public class HttpLoggingInterceptor extends OpenInterceptor {
    final String TAG = "HttpLogging";
    private boolean printLog ;
    public HttpLoggingInterceptor(){
        this(true);
    }
    public HttpLoggingInterceptor(boolean printLog) {
        this.printLog = printLog;
    }
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request requestTemp = chain.request();
        int hashCode = requestTemp.hashCode();
        if(printLog) {
            Log.d(TAG, "hashcode:" + hashCode);
        }
        Request.Builder requestBuild = requestTemp.newBuilder()
                .addHeader("device", new Gson().toJson(PhoneDevice.getDevice()))
                .addHeader("appVersion",  DeviceUtil.getAppVersionName(OpenApplication.Companion.getApplication()))
                .addHeader("os", DeviceUtil.isHarmonyOS()? "harmony" : "android");
        Log.d(TAG, "hashcode:" + hashCode);
        Request.Builder requestBuild = addHeaders(requestTemp);
        /*if(UserBean.getUser() != null){
            requestBuild.addHeader("token",UserBean.getUser().getToken());
        }*/
@@ -74,7 +57,7 @@
        try {
            logArrays.addAll(getRequestLog(request));
            int position = logArrays.size() +2;
            response = chain.proceed(request);
            response = proceed(chain,request);
            logArrays.addAll(getResponseLog(response));
            NetWorkCost netWorkCost = NetWorkListener.workCostMap.get(hashCode);
            if(netWorkCost != null) {
@@ -84,9 +67,7 @@
            new Thread(){
                @Override
                public void run() {
                    if(printLog) {
                        HttpPrintUtils.getInstance().printLog(logArrays, true);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应
                    }
                    HttpPrintUtils.getInstance().printLog(logArrays, true);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应
                }
            }.start();
        } catch (JSONException e) {
@@ -107,9 +88,7 @@
            new Thread(){
                @Override
                public void run() {
                    if(printLog) {
                        HttpPrintUtils.getInstance().printLog(logArrays, false);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应
                    }
                    HttpPrintUtils.getInstance().printLog(logArrays, false);//线程安全方法,需在新线程执行,避免阻塞当前线程,导致程序无响应
                }
            }.start();
            throw e;//抛出异常,用于请求接收信息
@@ -117,12 +96,16 @@
        return response;
    }
    protected Response proceed(Chain chain,Request request) throws IOException {
        return chain.proceed(request);
    }
    /**
     * 请求数据信息
     * @param request
     * @return
     */
    private ArrayList<String> getRequestLog(Request request) throws IOException, JSONException {
    protected ArrayList<String> getRequestLog(Request request) throws IOException, JSONException {
        RequestBody requestBody = request.body();
        ArrayList<String> logArrays = new ArrayList<>();
        String requestStartMessage = "--> " + request.method() + ' ' + URLDecoder.decode(request.url().toString() ,"UTF-8")+ ' ' ;
@@ -195,7 +178,7 @@
     * @throws IOException
     * @throws JSONException
     */
    private ArrayList<String> getResponseLog(Response response) throws IOException, JSONException {
    protected ArrayList<String> getResponseLog(Response response) throws IOException, JSONException {
        ArrayList<String> logArrays = new ArrayList<>();
        ResponseBody responseBody = response.body();
        long contentLength = responseBody.contentLength();
@@ -223,7 +206,16 @@
                if (contentLength != 0) {
                    String str = buffer.clone().readString(charset);
                    if(str.trim().indexOf("{") == 0) {
                        logArrays.add(new JSONObject(str).toString(4));
                        JSONObject object = new JSONObject(str);
                        if(object.has("body")){
                            try {
                                logArrays.add(new JSONObject(RSAUtils.decryptVerify(object.getString("body"))).toString(4));
                            } catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }else{
                            logArrays.add(object.toString(4));
                        }
                    }else if(str.trim().indexOf("[") == 0) {
                        logArrays.add(new JSONArray(str).toString(4));
                    }else{