mac
2023-11-20 734babb3a7348050fdffe845c560ba8b0b218152
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.getMessage() + "}");
                            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);
                }
            }