mac
2024-03-20 5ca2b601848c62607d88fee5afdae6af80f6eea7
app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
@@ -47,6 +47,8 @@
    private static volatile HttpClient sHttpClient;
    private static final boolean isAddToMemory = false;
    public static synchronized HttpClient getInstance() {
        if (sHttpClient == null) {
            synchronized (HttpClient.class) {
@@ -69,18 +71,16 @@
     * @return -
     */
    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);
        HdlLogLogic.print("http->发送->" + requestUrl + "\r\n" + body, isAddToMemory);
        //开发环境需要加(用来区分onpro和光伏app代码):HxHttp.builder().headers("x-lbs-version", "hdl-yt")
        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);
                    HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + str, isAddToMemory);
                }
            }
@@ -89,7 +89,7 @@
                HDLExceptionSubmitUtils.submit(requestUrl, body, e);
                if (callBack != null) {
                    callBack.onFailure(e);
                    System.out.println("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}");
                    HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
                }
            }
        });
@@ -109,15 +109,14 @@
        String requestUrl = HDLCloudUserApi.getRequestUrl(api);
        Gson gson = new Gson();
        System.out.println("http->发送->" + requestUrl + "\r\n" + body);
        HdlLogLogic.print("http->发送->" + requestUrl + "\r\n" + body, isAddToMemory);
        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);
                    HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + str, isAddToMemory);
                }
            }
@@ -126,7 +125,7 @@
                HDLExceptionSubmitUtils.submit(requestUrl, body, e);
                if (callBack != null) {
                    callBack.onFailure(e);
                    HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", true);
                    HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
                }
            }
        });
@@ -220,6 +219,17 @@
    }
    /**
     * get方法请求
     *
     * @param url           http地址
     * @param cloudCallBeak 回调
     */
    public void requestHttpGet(String url, CloudCallBeak<String> cloudCallBeak) {
        this.requestHttpServer(url, cloudCallBeak);
    }
    /**
     * 请求服务器的方法
     *
     * @param fullUrl        绝对请求地址
@@ -232,7 +242,7 @@
            @Override
            public void run() {
                try {
                    HdlLogLogic.print("http->发送->" + fullUrl + "\r\n" + json, false);
                    HdlLogLogic.print("http->发送->" + fullUrl + "\r\n" + json, isAddToMemory);
                    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)//请求的url
@@ -242,7 +252,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, false);
                            HdlLogLogic.print("http->回复->" + response.request().url() + "\r\n" + s, isAddToMemory);
                            baseSuccessCallBeak.onSuccess(httpResponsePack);
                        } else {
@@ -255,7 +265,7 @@
                        call.enqueue(new Callback() {//异步
                            @Override
                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                                HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), false);
                                HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), isAddToMemory);
                            }
                            @Override
@@ -265,7 +275,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, false);
                                    HdlLogLogic.print("http->回复->" + "\r\n" + s, isAddToMemory);
                                } else {
                                    baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message()));
                                }
@@ -274,9 +284,36 @@
                    }
                } catch (Exception e) {
                    baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage()));
                    HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), false);
                    HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), isAddToMemory);
                }
            }
        }).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();
    }
@@ -341,8 +378,6 @@
        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;
        return firstChar != '{' && firstChar != '[';
    }
}