wjc
2026-03-04 ee4f83fd6c214f67d6abcaf47e6bf5f913825e52
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>() {