From ee4f83fd6c214f67d6abcaf47e6bf5f913825e52 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 04 三月 2026 13:24:31 +0800
Subject: [PATCH] 2026年03月04日13:24:24

---
 app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java |  119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 113 insertions(+), 6 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
index 9ffd9f9..f2a01db 100644
--- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
+++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
@@ -9,12 +9,15 @@
 import com.google.gson.JsonObject;
 import com.google.gson.reflect.TypeToken;
 import com.hdl.hdlhttp.HxHttp;
+import com.hdl.hdlhttp.HxHttpConfig;
 import com.hdl.hdlhttp.utils.GsonConvert;
 import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi;
 import com.hdl.linkpm.sdk.core.exception.HDLException;
 import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader;
 import com.hdl.linkpm.sdk.core.response.HDLResponse;
 import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils;
+import com.hdl.photovoltaic.HDLApp;
+import com.hdl.photovoltaic.R;
 import com.hdl.photovoltaic.bean.HttpResponsePack;
 import com.hdl.photovoltaic.config.AppConfigManage;
 import com.hdl.photovoltaic.config.UserConfigManage;
@@ -26,6 +29,8 @@
 import com.hdl.photovoltaic.utils.LocalManageUtil;
 import com.hdl.photovoltaic.utils.Md5Utils;
 
+import org.jetbrains.annotations.NotNull;
+
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -33,11 +38,13 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 import io.reactivex.rxjava3.disposables.Disposable;
 import io.reactivex.rxjava3.functions.Consumer;
 import okhttp3.Call;
 import okhttp3.Callback;
+import okhttp3.ConnectionPool;
 import okhttp3.FormBody;
 import okhttp3.MediaType;
 import okhttp3.OkHttpClient;
@@ -64,19 +71,56 @@
         return sHttpClient;
     }
 
+    public static HDLException getException(HDLException e) {
+        try {
+            String str = TextUtils.isEmpty(getErrorSting(e.getCode())) ? e.getMsg() : getErrorSting(e.getCode());
+            return new HDLException(e.getCode(), str, e.getmExtra());
+        } catch (Exception exception) {
+            return e;
+        }
+    }
+
+    private static String getErrorSting(int code) {
+        try {
+            switch (code) {
+                case 1000:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.no_network_connection);
+                case 1001:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.parse_error);
+                case 1002:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.network_error);
+                case 1003:
+                case 1004:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.ssl_error);
+                case 1005:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.timeout_error);
+                case 4:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.signature_error_log_in_again);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return "";
+    }
+    private  final OkHttpClient client = new OkHttpClient.Builder()
+            .connectTimeout(30, TimeUnit.SECONDS)
+            .readTimeout(30, TimeUnit.SECONDS)
+            .writeTimeout(30, TimeUnit.SECONDS)
+//            .connectionPool(new ConnectionPool(10, 5, TimeUnit.MINUTES))  // 杩炴帴姹犲鐢�
+            .build();
     /**
      * 鑾峰彇json璧勬簮鐢ㄧ殑
      *
      * @param url      鍦板潃
      * @param callBack 鍥炶皟
      */
-    public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) {
+    public void requestJsonHttpGetSync(String url, CloudCallBeak<String> callBack) {
         if (TextUtils.isEmpty(url)) {
             if (callBack != null) {
                 callBack.onSuccess("");
             }
         }
-
         HdlThreadLogic.runSubThread(new Runnable() {
             @Override
             public void run() {
@@ -113,7 +157,70 @@
         });
 
     }
+    public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) {
+        if (TextUtils.isEmpty(url)) {
+            if (callBack != null) {
+                callBack.onSuccess("");
+            }
+            return;
+        }
 
+        Request request = new Request.Builder()
+                .url(url)
+                .addHeader("Accept-Language", UserConfigManage.getInstance().getCurrentAppLanguage())
+                .build();
+
+        //浣跨敤寮傛璇锋眰 enqueue()
+        client.newCall(request).enqueue(new Callback() {
+            @Override
+            public void onFailure(@NotNull Call call, @NotNull IOException e) {
+                if (callBack != null) {
+                    // 鍒囧洖涓荤嚎绋嬪洖璋冿紙鍥犱负 callBack 鍙兘娑夊強 UI 鎿嶄綔锛�
+                    HdlThreadLogic.runMainThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            callBack.onFailure(new HDLException(-20002, e.getMessage()));
+
+                        }
+                    });
+
+                }
+            }
+
+            @Override
+            public void onResponse(@NotNull Call call, @NotNull Response response) {
+                try  {  // 鑷姩鍏抽棴
+                    if (response.isSuccessful() && response.body() != null) {
+                        String str = response.body().string();
+                        if (callBack != null) {
+                            // 猸� 鍒囧洖涓荤嚎绋�
+                           HdlThreadLogic.runMainThread(() ->
+                                    callBack.onSuccess(str)
+                            );
+                        }
+                    } else {
+                        if (callBack != null) {
+                            HdlThreadLogic.runMainThread(() ->
+                                    callBack.onSuccess("")
+                            );
+                        }
+                    }
+                } catch (IOException e) {
+                    if (callBack != null) {
+                        HdlThreadLogic.runMainThread(new Runnable() {
+                            @Override
+                            public void run() {
+                                callBack.onFailure(new HDLException(-20002, e.getMessage()));
+
+                            }
+                        });
+                    }
+                }finally {
+                    response.close();
+                }
+            }
+        });
+    }
 
     /**
      * 璇锋眰鏈嶅姟鍣�(get)
@@ -140,7 +247,7 @@
             public void onFailure(HDLException e) {
                 HDLExceptionSubmitUtils.submit(requestUrl, "", e);
                 if (callBack != null) {
-                    callBack.onFailure(e);
+                    callBack.onFailure(getException(e));
                     HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
                 }
             }
@@ -175,7 +282,7 @@
             public void onFailure(HDLException e) {
                 HDLExceptionSubmitUtils.submit(requestUrl, body, e);
                 if (callBack != null) {
-                    callBack.onFailure(e);
+                    callBack.onFailure(getException(e));
 //                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
                 }
             }
@@ -211,7 +318,7 @@
             public void onFailure(HDLException e) {
                 HDLExceptionSubmitUtils.submit(requestUrl, body, e);
                 if (callBack != null) {
-                    callBack.onFailure(e);
+                    callBack.onFailure(getException(e));
 //                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
                 }
             }
@@ -228,7 +335,7 @@
     public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) {
         return HxHttp.builder()
                 .url(url)
-                .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1)
+                .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1).headers(HDLSmartHeader.NOAUTHENTICATION, 1)
                 .build()
                 .download()
                 .subscribe(new Consumer<ResponseBody>() {

--
Gitblit v1.8.0