From b9cc7390e8e8ce64c41c26fb369c98ce669d660c Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 07 五月 2025 15:02:30 +0800 Subject: [PATCH] Merge branch '1.2.0' --- app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 68 ++++++++++++++++++++++++++-------- 1 files changed, 52 insertions(+), 16 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 ab3bab6..075d3db 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; @@ -62,6 +65,39 @@ } 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 ""; } /** @@ -124,7 +160,7 @@ * @return - */ private Disposable requestGet(String requestUrl, CloudCallBeak<String> callBack) { - HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n", isAddToMemory); +// HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n", isAddToMemory); //寮�鍙戠幆澧冮渶瑕佸姞(鐢ㄦ潵鍖哄垎onpro鍜屽厜浼廰pp浠g爜):HxHttp.builder().headers("x-lbs-version", "hdl-yt") return HxHttp.builder().url(requestUrl).build().get().subscribeWith(new HDLResponse<String>() { @Override @@ -132,7 +168,7 @@ if (callBack != null) { callBack.onSuccess(str); // System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); } } @@ -140,7 +176,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); } } @@ -159,7 +195,7 @@ */ private Disposable requestPost(String api, String body, CloudCallBeak<String> callBack) { String requestUrl = HDLCloudUserApi.getRequestUrl(api); - HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory); +// HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory); //寮�鍙戠幆澧冮渶瑕佸姞(鐢ㄦ潵鍖哄垎onpro鍜屽厜浼廰pp浠g爜):HxHttp.builder().headers("x-lbs-version", "hdl-yt") return HxHttp.builder().url(requestUrl).raw(body).build().post().subscribeWith(new HDLResponse<String>() { @Override @@ -167,7 +203,7 @@ if (callBack != null) { callBack.onSuccess(str); // System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); } } @@ -175,8 +211,8 @@ public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, body, e); if (callBack != null) { - callBack.onFailure(e); - HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); + callBack.onFailure(getException(e)); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); } } }); @@ -196,14 +232,14 @@ String requestUrl = HDLCloudUserApi.getRequestUrl(api); - HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory); +// HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory); return HxHttp.builder().url(requestUrl).file(body).build().post().subscribeWith(new HDLResponse<String>() { @Override public void onResponse(String str) { if (callBack != null) { callBack.onSuccess(str); // System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); } } @@ -211,8 +247,8 @@ public void onFailure(HDLException e) { HDLExceptionSubmitUtils.submit(requestUrl, body, e); if (callBack != null) { - callBack.onFailure(e); - HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); + callBack.onFailure(getException(e)); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); } } }); @@ -328,7 +364,7 @@ @Override public void run() { try { - HdlLogLogic.print("http---鍙戦��---" + fullUrl + "\r\n" + json, isAddToMemory); +// HdlLogLogic.print("http---鍙戦��---" + fullUrl + "\r\n" + json, isAddToMemory); OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), getJson(json)); final Request request = new Request.Builder().url(fullUrl)//璇锋眰鐨剈rl @@ -338,7 +374,7 @@ if (response.isSuccessful()) { String s = Objects.requireNonNull(response.body()).string(); HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class); - HdlLogLogic.print("http---鍥炲---" + response.request().url() + "\r\n" + s, isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + response.request().url() + "\r\n" + s, isAddToMemory); baseSuccessCallBeak.onSuccess(httpResponsePack); } else { @@ -351,7 +387,7 @@ call.enqueue(new Callback() {//寮傛 @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { - HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory); } @Override @@ -361,7 +397,7 @@ String s = Objects.requireNonNull(response.body()).string(); HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class); baseSuccessCallBeak.onSuccess(httpResponsePack); - HdlLogLogic.print("http---鍥炲---" + "\r\n" + s, isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + "\r\n" + s, isAddToMemory); } else { baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message())); } @@ -370,7 +406,7 @@ } } catch (Exception e) { baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage())); - HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory); +// HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory); } } -- Gitblit v1.8.0