From f10bd384ab2c5e459b9bf20766970615e429b4ec Mon Sep 17 00:00:00 2001 From: mac <user@users-MacBook-Pro.local> Date: 星期四, 16 十一月 2023 16:53:22 +0800 Subject: [PATCH] 备份一下代码 --- app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java | 73 ++++++++++++++++++++++++++++++++---- 1 files changed, 64 insertions(+), 9 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 eab83ff..e4f21cd 100644 --- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java +++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java @@ -25,6 +25,7 @@ import com.hdl.photovoltaic.other.HdlLogLogic; import com.hdl.photovoltaic.utils.HDLMD5Utils; +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -73,7 +74,7 @@ String requestUrl = HDLCloudUserApi.getRequestUrl(api); Gson gson = new Gson(); - System.out.println("鍙戦��->" + requestUrl + "\r\n" + body); + System.out.println("http->鍙戦��->" + requestUrl + "\r\n" + body); return HxHttp.builder() .url(requestUrl) .raw(body) @@ -84,8 +85,8 @@ public void onResponse(String str) { if (callBack != null) { callBack.onSuccess(str); -// System.out.println("鍥炲->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); - System.out.println("鍥炲->" + requestUrl + "\r\n" + str); +// System.out.println("http->鍥炲->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); + System.out.println("http->鍥炲->" + requestUrl + "\r\n" + str); } } @@ -94,7 +95,49 @@ HDLExceptionSubmitUtils.submit(requestUrl, body, e); if (callBack != null) { callBack.onFailure(e); - System.out.println("鍥炲->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); + 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 requestFile(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); } } }); @@ -112,6 +155,18 @@ public Disposable requestHttp(String api, String body, CloudCallBeak<String> callBack) { return request(api, body, callBack); } + + /** + * 璇锋眰鏈嶅姟鍣紙涓婁紶鏂囦欢锛� + * + * @param api 璇锋眰鎺ュ彛 + * @param body 璇锋眰鍙傛暟(json) + * @param callBack 鍥炶皟 + */ + public Disposable requestHttpFile(String api, File body, CloudCallBeak<String> callBack) { + return requestFile(api, body, callBack); + } + /** * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶(宸插純鐢�) @@ -154,7 +209,7 @@ @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() @@ -167,7 +222,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); + HdlLogLogic.print("http->鍥炲->" + response.request().url() + "\r\n" + s, false); baseSuccessCallBeak.onSuccess(httpResponsePack); } else { @@ -180,7 +235,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 @@ -190,7 +245,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())); } @@ -199,7 +254,7 @@ } } 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); } } -- Gitblit v1.8.0