From 0f91b37f7711481b9671409d0de08873d77060eb Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期三, 20 十二月 2023 17:24:31 +0800 Subject: [PATCH] 2023年12月20日17:24:20 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 194 +++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 157 insertions(+), 37 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 69222d3..766f4ba 100644 --- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java +++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java @@ -12,9 +12,8 @@ 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.user.bean.HDLLoginBean; -import com.hdl.linkpm.sdk.user.callback.ILoginCallBack; import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; import com.hdl.photovoltaic.bean.HttpResponsePack; import com.hdl.photovoltaic.config.AppConfigManage; @@ -22,8 +21,9 @@ import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak; import com.hdl.photovoltaic.listener.CloudCallBeak; import com.hdl.photovoltaic.other.HdlLogLogic; -import com.hdl.photovoltaic.utils.HDLMD5Utils; +import com.hdl.photovoltaic.utils.Md5Utils; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -32,6 +32,7 @@ import java.util.Objects; import io.reactivex.rxjava3.disposables.Disposable; +import io.reactivex.rxjava3.functions.Consumer; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; @@ -40,6 +41,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import okhttp3.ResponseBody; public class HttpClient { @@ -62,35 +64,109 @@ * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶 * * @param api 璇锋眰鎺ュ彛 - * @param body 璇锋眰鍙傛暟 + * @param body 璇锋眰鍙傛暟(appKey,timestamp,sign杩欎笁涓弬鏁板唴閮ㄤ細鑷姩娣诲姞)) * @param callBack 鍥炶皟 * @return - */ - private Disposable request(String api, String body, CloudCallBeak<String> callBack) { + private Disposable requestPost(String api, String body, CloudCallBeak<String> callBack) { + + String requestUrl = HDLCloudUserApi.getRequestUrl(api); + Gson gson = new Gson(); + System.out.println("http->鍙戦��->" + requestUrl + "\r\n" + body); + 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()); + System.out.println("http->鍥炲->" + requestUrl + "\r\n" + str); + } + } + + @Override + public void onFailure(HDLException e) { + HDLExceptionSubmitUtils.submit(requestUrl, body, e); + if (callBack != null) { + callBack.onFailure(e); + System.out.println("http->鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); + } + } + }); + + } + + /** + * 璇锋眰鏈嶅姟鍣� + * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶 + * + * @param api 璇锋眰鎺ュ彛 + * @param body 璇锋眰鍙傛暟(appKey,timestamp,sign杩欎笁涓弬鏁板唴閮ㄤ細鑷姩娣诲姞)) + * @param callBack 鍥炶皟 + * @return - + */ + private Disposable requestFilePost(String api, File body, CloudCallBeak<String> callBack) { + + + String requestUrl = HDLCloudUserApi.getRequestUrl(api); + Gson gson = new Gson(); + System.out.println("http->鍙戦��->" + requestUrl + "\r\n" + body); + 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, true); + } + } + + @Override + 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() + "}", true); + } + } + }); + + } + + + /** + * 涓嬭浇鏂囦欢 + * + * @param callBack - + */ + public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) { return HxHttp.builder() - .url(requestUrl) - .raw(body) + .url(url) + .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1) .build() - .post() - .subscribeWith(new HDLResponse<String>() { + .download() + .subscribe(new Consumer<ResponseBody>() { @Override - public void onResponse(String str) { + public void accept(ResponseBody responseBody) throws Exception { if (callBack != null) { - callBack.onSuccess(str); + callBack.onSuccess(responseBody); } } - + }, new Consumer<Throwable>() { @Override - public void onFailure(HDLException e) { - HDLExceptionSubmitUtils.submit(requestUrl, body, e); + public void accept(Throwable throwable) throws Exception { + /** + * 缃戠粶璇锋眰澶辫触 + * 鍏蜂綋鍙互鑷繁瀹炵幇 + */ + HDLExceptionSubmitUtils.submit(url, null, throwable); if (callBack != null) { - callBack.onFailure(e); + callBack.onFailure(new HDLException(403, throwable.getMessage())); } } }); - } + /** * 璇锋眰鏈嶅姟鍣� @@ -100,8 +176,20 @@ * @param callBack 鍥炶皟 */ public Disposable requestHttp(String api, String body, CloudCallBeak<String> callBack) { - return request(api, body, callBack); + return requestPost(api, body, callBack); } + + /** + * 涓婁紶鏂囦欢 + * + * @param api 璇锋眰鎺ュ彛 + * @param body 璇锋眰鍙傛暟(json) + * @param callBack 鍥炶皟 + */ + public Disposable uploadingFile(String api, File body, CloudCallBeak<String> callBack) { + return requestFilePost(api, body, callBack); + } + /** * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶(宸插純鐢�) @@ -114,7 +202,7 @@ @Deprecated private void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl; - this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); + this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); } /** @@ -128,8 +216,19 @@ */ @Deprecated private void requestFullHttp(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { - this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); + 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); + } + /** * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶 @@ -139,25 +238,22 @@ * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�) * @param isExecute 鏄惁鏄悓姝�(true=鍚屾,false=寮傛) */ - private void requestHttps(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { + private void requestHttpServer(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { new Thread(new Runnable() { @Override public void run() { try { - HdlLogLogic.print("http->鍙戦��->", fullUrl + "\r\n" + json); + HdlLogLogic.print("http->鍙戦��->" + fullUrl + "\r\n" + json, false); 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 - .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()) - .post(requestBody) - .build(); + final Request request = new Request.Builder().url(fullUrl)//璇锋眰鐨剈rl + .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()).post(requestBody).build(); if (isExecute) { Response response = okHttpClient.newCall(request).execute();//鍚屾 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); + HdlLogLogic.print("http->鍥炲->" + response.request().url() + "\r\n" + s, false); baseSuccessCallBeak.onSuccess(httpResponsePack); } else { @@ -170,7 +266,7 @@ call.enqueue(new Callback() {//寮傛 @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { - HdlLogLogic.print("http->鍥炲->", "\r\n" + e.getMessage()); + HdlLogLogic.print("http->鍥炲->" + "\r\n" + e.getMessage(), false); } @Override @@ -180,7 +276,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); + HdlLogLogic.print("http->鍥炲->" + "\r\n" + s, false); } else { baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message())); } @@ -189,9 +285,36 @@ } } catch (Exception e) { baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage())); - HdlLogLogic.print("http->鍥炲->", "\r\n" + e.getMessage()); + HdlLogLogic.print("http->鍥炲->" + "\r\n" + e.getMessage(), false); } + } + }).start(); + } + + /** + * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶 + * + * @param url 缁濆璇锋眰鍦板潃 + */ + private void requestHttpServer(String url, CloudCallBeak<String> cloudCallBeak) { + new Thread(new Runnable() { + @Override + public void run() { + OkHttpClient okHttpClient = new OkHttpClient(); + final Request request = new Request.Builder().url(url).build(); + try { + Response response = okHttpClient.newCall(request).execute(); + String s = Objects.requireNonNull(response.body()).string(); + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(s); + } + } catch (Exception e) { + if (cloudCallBeak != null) { + cloudCallBeak.onSuccess(e.getMessage()); + } + + } } }).start(); } @@ -220,9 +343,8 @@ * @param json 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret */ private String getSign(JsonObject json, String appSecret) { - String builder = jsonToUrlParameter(json) + - appSecret; - return HDLMD5Utils.encodeMD5(builder); + String builder = jsonToUrlParameter(json) + appSecret; + return Md5Utils.encodeMD5(builder); } @@ -253,13 +375,11 @@ * @return - */ private boolean IfValueNeedSign(String valueStr) { - if (TextUtils.isEmpty(valueStr)) - return false; + if (TextUtils.isEmpty(valueStr)) return false; final char[] strChar = valueStr.substring(0, 1).toCharArray(); final char firstChar = strChar[0]; //System.out.println("getJSONType firstChar = "+firstChar); - if (firstChar != '{' && firstChar != '[') - return true; + if (firstChar != '{' && firstChar != '[') return true; return false; } -- Gitblit v1.8.0