From 1793cca452c8d905ec64d26d2bfad6821f5e0c4c Mon Sep 17 00:00:00 2001 From: wjc <1243177876@qq.com> Date: 星期三, 09 四月 2025 11:10:36 +0800 Subject: [PATCH] 2025年04月09日11:10:33 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 183 ++++++++++++++++++++++++++++++++++++++------- 1 files changed, 152 insertions(+), 31 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 d1ddd99..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,18 +9,24 @@ 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; import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak; import com.hdl.photovoltaic.listener.CloudCallBeak; +import com.hdl.photovoltaic.other.HdlESLocalJsonLogic; import com.hdl.photovoltaic.other.HdlLogLogic; +import com.hdl.photovoltaic.other.HdlThreadLogic; +import com.hdl.photovoltaic.utils.LocalManageUtil; import com.hdl.photovoltaic.utils.Md5Utils; import java.io.File; @@ -47,7 +53,7 @@ private static volatile HttpClient sHttpClient; - private static final boolean isAddToMemory=false; + private static final boolean isAddToMemory = false; public static synchronized HttpClient getInstance() { if (sHttpClient == null) { @@ -61,8 +67,125 @@ 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 ""; + } + /** - * 璇锋眰鏈嶅姟鍣� + * 鑾峰彇json璧勬簮鐢ㄧ殑 + * + * @param url 鍦板潃 + * @param callBack 鍥炶皟 + */ + public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) { + if (TextUtils.isEmpty(url)) { + if (callBack != null) { + callBack.onSuccess(""); + } + } + + HdlThreadLogic.runSubThread(new Runnable() { + @Override + public void run() { + OkHttpClient client = new OkHttpClient(); + Request request = new Request.Builder() + .url(url) + .addHeader("Accept-Language", UserConfigManage.getInstance().getCurrentAppLanguage()) + .build(); + + try (Response response = client.newCall(request).execute()) { + if (response.isSuccessful()) { + if (response.body() == null) { + if (callBack != null) { + callBack.onSuccess(""); + } + } else { + String str = response.body().string(); + if (callBack != null) { + callBack.onSuccess(str); + } + } + + } else { + if (callBack != null) { + callBack.onSuccess(""); + } + } + } catch (Exception e) { + if (callBack != null) { + callBack.onFailure(new HDLException(-20002, e.getMessage())); + } + } + } + }); + + } + + + /** + * 璇锋眰鏈嶅姟鍣�(get) + * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶 + * + * @param requestUrl 璇锋眰缁濆鍦板潃鎺ュ彛 + * @param callBack 鍥炶皟 + * @return - + */ + private Disposable requestGet(String requestUrl, CloudCallBeak<String> callBack) { +// 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 + 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); + } + } + + @Override + public void onFailure(HDLException e) { + HDLExceptionSubmitUtils.submit(requestUrl, "", e); + if (callBack != null) { + callBack.onFailure(getException(e)); + HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); + } + } + }); + + } + + /** + * 璇锋眰鏈嶅姟鍣�(post) * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶 * * @param api 璇锋眰鎺ュ彛 @@ -72,14 +195,15 @@ */ 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 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); +// System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); } } @@ -87,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); } } }); @@ -108,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); +// System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); +// HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory); } } @@ -123,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); } } }); @@ -165,9 +289,18 @@ }); } + /** + * 璇锋眰(get)鏈嶅姟鍣� + * + * @param requestUrl 璇锋眰缁濆鍦板潃鎺ュ彛 + * @param callBack 鍥炶皟 + */ + public Disposable requestHttpGet(String requestUrl, CloudCallBeak<String> callBack) { + return requestGet(requestUrl, callBack); + } /** - * 璇锋眰鏈嶅姟鍣� + * 璇锋眰(post)鏈嶅姟鍣� * * @param api 璇锋眰鎺ュ彛 * @param body 璇锋眰鍙傛暟(json) @@ -217,16 +350,6 @@ this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); } - /** - * get鏂规硶璇锋眰 - * - * @param url http鍦板潃 - * @param cloudCallBeak 鍥炶皟 - */ - public void requestHttpGet(String url, CloudCallBeak<String> cloudCallBeak) { - this.requestHttpServer(url, cloudCallBeak); - } - /** * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶 @@ -241,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 @@ -251,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 { @@ -264,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 @@ -274,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())); } @@ -283,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); } } @@ -377,8 +500,6 @@ final char[] strChar = valueStr.substring(0, 1).toCharArray(); final char firstChar = strChar[0]; //System.out.println("getJSONType firstChar = "+firstChar); - if (firstChar != '{' && firstChar != '[') return true; - - return false; + return firstChar != '{' && firstChar != '['; } } -- Gitblit v1.8.0