From 22494af577e21a930abef309f2f60c03c9615bd1 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期五, 07 七月 2023 18:16:55 +0800
Subject: [PATCH] 2023年07月07日18:16:52

---
 app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java |   87 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 73 insertions(+), 14 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 8d2c3cd..850f6cb 100644
--- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
+++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
@@ -8,10 +8,19 @@
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
 import com.google.gson.reflect.TypeToken;
+import com.hdl.hdlhttp.HxHttp;
 import com.hdl.hdlhttp.utils.GsonConvert;
-import com.hdl.photovoltaic.bean.ResponsePack;
-import com.hdl.photovoltaic.config.ConfigManagement;
+import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi;
+import com.hdl.linkpm.sdk.core.exception.HDLException;
+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;
+import com.hdl.photovoltaic.config.UserConfigManage;
 import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
+import com.hdl.photovoltaic.other.HdlLogLogic;
 import com.hdl.photovoltaic.utils.HDLMD5Utils;
 
 import java.io.IOException;
@@ -21,6 +30,7 @@
 import java.util.Map;
 import java.util.Objects;
 
+import io.reactivex.rxjava3.disposables.Disposable;
 import okhttp3.Call;
 import okhttp3.Callback;
 import okhttp3.FormBody;
@@ -47,6 +57,52 @@
     }
 
     /**
+     * 璇锋眰鏈嶅姟鍣�
+     * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟
+     * @param callBack 鍥炶皟
+     * @return -
+     */
+    private Disposable request(String api, String body, BaseSuccessFailureCallBeak callBack) {
+        String requestUrl = HDLCloudUserApi.getRequestUrl(api);
+        return HxHttp.builder()
+                .url(requestUrl)
+                .raw(body)
+                .build()
+                .post()
+                .subscribeWith(new HDLResponse<HttpResponsePack>() {
+                    @Override
+                    public void onResponse(HttpResponsePack response) {
+                        if (callBack != null) {
+                            callBack.onSuccess(response);
+                        }
+                    }
+
+                    @Override
+                    public void onFailure(HDLException e) {
+                        HDLExceptionSubmitUtils.submit(requestUrl, body, e);
+                        if (callBack != null) {
+                            callBack.onFailure(e);
+                        }
+                    }
+                });
+
+    }
+
+    /**
+     * 璇锋眰鏈嶅姟鍣�
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟(json)
+     * @param callBack 鍥炶皟
+     */
+    public Disposable requestHttp(String api, String body, BaseSuccessFailureCallBeak callBack) {
+        return request(api, body, callBack);
+    }
+
+    /**
      * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
      *
      * @param requestUrl     璇锋眰鎺ュ彛
@@ -55,7 +111,7 @@
      * @param isExecute      鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
      */
     public void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
-        String fullUrl = ConfigManagement.getUserRegionUrl() + requestUrl;
+        String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl;
         this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
     }
 
@@ -71,7 +127,6 @@
         this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
     }
 
-
     /**
      * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
      *
@@ -85,19 +140,22 @@
             @Override
             public void run() {
                 try {
+                    HdlLogLogic.print("http->鍙戦��->", fullUrl + "\r\n" + json);
                     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", "accessToken")
+                            .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();
-                            ResponsePack responsePack = new Gson().fromJson(s, ResponsePack.class);
-                            baseSuccessCallBeak.onSuccess(responsePack);
+                            HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class);
+                            HdlLogLogic.print("http->鍥炲->", response.request().url() + "\r\n" + s);
+                            baseSuccessCallBeak.onSuccess(httpResponsePack);
+
                         } else {
                             //throw new IOException("Unexpected code " + response);
                             baseSuccessCallBeak.onFailure(new Exception());
@@ -108,7 +166,7 @@
                         call.enqueue(new Callback() {//寮傛
                             @Override
                             public void onFailure(@NonNull Call call, @NonNull IOException e) {
-                                System.out.println("澶辫触" + e.toString());
+                                HdlLogLogic.print("http->鍥炲->", "\r\n" + e.getMessage());
                             }
 
                             @Override
@@ -116,8 +174,9 @@
                                 System.out.println("url杩炴帴淇℃伅" + response.code());
                                 if (response.code() == 200) {
                                     String s = Objects.requireNonNull(response.body()).string();
-                                    ResponsePack responsePack = new Gson().fromJson(s, ResponsePack.class);
-                                    baseSuccessCallBeak.onSuccess(responsePack);
+                                    HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class);
+                                    baseSuccessCallBeak.onSuccess(httpResponsePack);
+                                    HdlLogLogic.print("http->鍥炲->", "\r\n" + s);
                                 } else {
                                     baseSuccessCallBeak.onFailure(new Exception());
                                 }
@@ -126,10 +185,10 @@
                     }
                 } catch (Exception e) {
                     baseSuccessCallBeak.onFailure(e);
+                    HdlLogLogic.print("http->鍥炲->", "\r\n" + e.getMessage());
                 }
 
             }
-
         }).start();
     }
 
@@ -138,8 +197,8 @@
      */
     private String getJson(String json) {
         final String timestamp = String.valueOf(System.currentTimeMillis());
-        final String appKey = ConfigManagement.getAppKey();
-        final String appSecret = ConfigManagement.getAppSecret();
+        final String appKey = AppConfigManage.getAppKey();
+        final String appSecret = AppConfigManage.getAppSecret();
         JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
         if (jsonObject == null) {
             jsonObject = new JsonObject();

--
Gitblit v1.8.0