From bed9201624d77443c6151f47d34191c84ffc0256 Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 14 六月 2023 16:41:49 +0800
Subject: [PATCH] 2023年06月14日16:41:39
---
app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 205 insertions(+), 0 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 e82d14e..9a4c9ab 100644
--- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
+++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
@@ -1,4 +1,209 @@
package com.hdl.photovoltaic.internet;
+import android.net.Uri;
+import android.text.TextUtils;
+
+import androidx.annotation.NonNull;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.gson.reflect.TypeToken;
+import com.hdl.hdlhttp.utils.GsonConvert;
+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;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+
public class HttpClient {
+
+ private static volatile HttpClient sHttpClient;
+
+ public static synchronized HttpClient getInstance() {
+ if (sHttpClient == null) {
+ synchronized (HttpClient.class) {
+ if (sHttpClient == null) {
+ sHttpClient = new HttpClient();
+ }
+ }
+
+ }
+ return sHttpClient;
+ }
+
+ /**
+ * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
+ *
+ * @param requestUrl 璇锋眰鎺ュ彛
+ * @param json 璇锋眰鏁版嵁
+ * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+ * @param isExecute 鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+ */
+ public void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
+ String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl;
+ this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
+ }
+
+ /**
+ * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶(鐩墠鍙敤鍦ㄨ幏鍙栬幏鍙栬处鍙峰尯鍩熶俊鎭�)
+ *
+ * @param fullUrl 缁濆鍦板潃(鍦板潃+鎺ュ彛)
+ * @param json 璇锋眰鏁版嵁
+ * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+ * @param isExecute 鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+ */
+ public void requestFullHttp(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
+ this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
+ }
+
+ /**
+ * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
+ *
+ * @param fullUrl 缁濆璇锋眰鍦板潃
+ * @param json 璇锋眰鏁版嵁
+ * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+ * @param isExecute 鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+ */
+ private void requestHttps(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);
+ 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();
+ 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);
+ baseSuccessCallBeak.onSuccess(httpResponsePack);
+ HdlLogLogic.print("http->鍥炲->", response.request().url()+"\r\n" + s);
+ } else {
+ //throw new IOException("Unexpected code " + response);
+ baseSuccessCallBeak.onFailure(new Exception());
+ }
+ } else {
+
+ Call call = okHttpClient.newCall(request);
+ 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
+ public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
+ System.out.println("url杩炴帴淇℃伅" + response.code());
+ if (response.code() == 200) {
+ String s = Objects.requireNonNull(response.body()).string();
+ HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class);
+ baseSuccessCallBeak.onSuccess(httpResponsePack);
+ HdlLogLogic.print("http->鍥炲->", "\r\n" + s);
+ } else {
+ baseSuccessCallBeak.onFailure(new Exception());
+ }
+ }
+ });
+ }
+ } catch (Exception e) {
+ baseSuccessCallBeak.onFailure(e);
+ HdlLogLogic.print("http->鍥炲->", "\r\n" + e.getMessage());
+ }
+
+ }
+ }).start();
+ }
+
+ /**
+ * 娣诲姞sign瀛楁
+ */
+ private String getJson(String json) {
+ final String timestamp = String.valueOf(System.currentTimeMillis());
+ final String appKey = AppConfigManage.getAppKey();
+ final String appSecret = AppConfigManage.getAppSecret();
+ JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
+ if (jsonObject == null) {
+ jsonObject = new JsonObject();
+ }
+ jsonObject.addProperty("appKey", appKey);
+ jsonObject.addProperty("timestamp", timestamp);
+ jsonObject.addProperty("sign", getSign(jsonObject, appSecret));
+
+ return jsonObject.toString();
+ }
+
+ /**
+ * 闇�瑕佹寜瀛楁瘝鎺掑簭
+ *
+ * @param json 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret
+ */
+ private String getSign(JsonObject json, String appSecret) {
+ String builder = jsonToUrlParameter(json) +
+ appSecret;
+ return HDLMD5Utils.encodeMD5(builder);
+ }
+
+
+ private String jsonToUrlParameter(JsonObject object) {
+ final Map<String, String> map = GsonConvert.getGson().fromJson(object, new TypeToken<Map<String, String>>() {
+ }.getType());
+ final Uri.Builder builder = new Uri.Builder();
+ List<String> list = new ArrayList<>(map.keySet());
+ Collections.sort(list);
+ for (String key : list) {
+ //鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷�
+ if (IfValueNeedSign(map.get(key))) {
+ builder.appendQueryParameter(key, map.get(key));
+// HDLSDKLog.e("瑕佺鍚嶏細" + key + " 锛�" + map.get(key));
+ } else {
+// HDLSDKLog.e("涓嶉渶瑕佺鍚嶏細" + key + " 锛�" + map.get(key));
+ }
+ }
+ return builder.build().getQuery();
+ }
+
+ /**
+ * 鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷�
+ * 绌哄瓧绗︿覆涓嶅弬涓�
+ * 鏁扮粍,闆嗗悎,瀵硅薄涓嶅弬涓�
+ *
+ * @param valueStr -
+ * @return -
+ */
+ private boolean IfValueNeedSign(String valueStr) {
+ 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;
+
+ return false;
+ }
}
--
Gitblit v1.8.0