| | |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.hdlhttp.HxHttp; |
| | | import com.hdl.hdlhttp.HxHttpConfig; |
| | | import com.hdl.hdlhttp.utils.GsonConvert; |
| | | import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi; |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader; |
| | | import com.hdl.linkpm.sdk.core.response.HDLResponse; |
| | | import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | import com.hdl.photovoltaic.R; |
| | | 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.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlESLocalJsonLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.utils.LocalManageUtil; |
| | | import com.hdl.photovoltaic.utils.Md5Utils; |
| | | |
| | | import java.io.File; |
| | |
| | | return sHttpClient; |
| | | } |
| | | |
| | | public static HDLException getException(HDLException e) { |
| | | try { |
| | | String str = TextUtils.isEmpty(getErrorSting(e.getCode())) ? e.getMsg() : getErrorSting(e.getCode()); |
| | | return new HDLException(e.getCode(), str, e.getmExtra()); |
| | | } catch (Exception exception) { |
| | | return e; |
| | | } |
| | | } |
| | | |
| | | private static String getErrorSting(int code) { |
| | | try { |
| | | switch (code) { |
| | | case 1000: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.no_network_connection); |
| | | case 1001: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.parse_error); |
| | | case 1002: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.network_error); |
| | | case 1003: |
| | | case 1004: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.ssl_error); |
| | | case 1005: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.timeout_error); |
| | | case 4: |
| | | return HDLApp.getInstance().getAppLocaleContext().getString(R.string.signature_error_log_in_again); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器 |
| | | * 获取json资源用的 |
| | | * |
| | | * @param url 地址 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) { |
| | | if (TextUtils.isEmpty(url)) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | } |
| | | |
| | | HdlThreadLogic.runSubThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | OkHttpClient client = new OkHttpClient(); |
| | | Request request = new Request.Builder() |
| | | .url(url) |
| | | .addHeader("Accept-Language", UserConfigManage.getInstance().getCurrentAppLanguage()) |
| | | .build(); |
| | | |
| | | try (Response response = client.newCall(request).execute()) { |
| | | if (response.isSuccessful()) { |
| | | if (response.body() == null) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | } else { |
| | | String str = response.body().string(); |
| | | if (callBack != null) { |
| | | callBack.onSuccess(str); |
| | | } |
| | | } |
| | | |
| | | } else { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | if (callBack != null) { |
| | | callBack.onFailure(new HDLException(-20002, e.getMessage())); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 请求服务器(get) |
| | | * 底层方法,引用库的方法 |
| | | * |
| | | * @param requestUrl 请求绝对地址接口 |
| | | * @param callBack 回调 |
| | | * @return - |
| | | */ |
| | | private Disposable requestGet(String requestUrl, CloudCallBeak<String> callBack) { |
| | | // HdlLogLogic.print("http---发送---" + requestUrl + "\r\n", isAddToMemory); |
| | | //开发环境需要加(用来区分onpro和光伏app代码):HxHttp.builder().headers("x-lbs-version", "hdl-yt") |
| | | return HxHttp.builder().url(requestUrl).build().get().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, isAddToMemory); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, "", e); |
| | | if (callBack != null) { |
| | | callBack.onFailure(getException(e)); |
| | | HdlLogLogic.print("http---回复---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器(post) |
| | | * 底层方法,引用库的方法 |
| | | * |
| | | * @param api 请求接口 |
| | |
| | | */ |
| | | private Disposable requestPost(String api, String body, CloudCallBeak<String> callBack) { |
| | | String requestUrl = HDLCloudUserApi.getRequestUrl(api); |
| | | HdlLogLogic.print("http->发送->" + requestUrl + "\r\n" + body, isAddToMemory); |
| | | return HxHttp.builder().headers("x-lbs-version", "hdl-yt").url(requestUrl).raw(body).build().post().subscribeWith(new HDLResponse<String>() { |
| | | // 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()); |
| | | HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + str, isAddToMemory); |
| | | // System.out.println("http---回复---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); |
| | | // HdlLogLogic.print("http---回复---" + requestUrl + "\r\n" + str, isAddToMemory); |
| | | } |
| | | } |
| | | |
| | |
| | | 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() + "}", isAddToMemory); |
| | | callBack.onFailure(getException(e)); |
| | | // HdlLogLogic.print("http---回复---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); |
| | | } |
| | | } |
| | | }); |
| | |
| | | |
| | | |
| | | String requestUrl = HDLCloudUserApi.getRequestUrl(api); |
| | | HdlLogLogic.print("http->发送->" + requestUrl + "\r\n" + body, isAddToMemory); |
| | | // 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, isAddToMemory); |
| | | // System.out.println("http---回复---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); |
| | | // HdlLogLogic.print("http---回复---" + requestUrl + "\r\n" + str, isAddToMemory); |
| | | } |
| | | } |
| | | |
| | |
| | | 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() + "}", isAddToMemory); |
| | | callBack.onFailure(getException(e)); |
| | | // HdlLogLogic.print("http---回复---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory); |
| | | } |
| | | } |
| | | }); |
| | |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 请求(get)服务器 |
| | | * |
| | | * @param requestUrl 请求绝对地址接口 |
| | | * @param callBack 回调 |
| | | */ |
| | | public Disposable requestHttpGet(String requestUrl, CloudCallBeak<String> callBack) { |
| | | return requestGet(requestUrl, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器 |
| | | * 请求(post)服务器 |
| | | * |
| | | * @param api 请求接口 |
| | | * @param body 请求参数(json) |
| | |
| | | this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | } |
| | | |
| | | /** |
| | | * get方法请求 |
| | | * |
| | | * @param url http地址 |
| | | * @param cloudCallBeak 回调 |
| | | */ |
| | | public void requestHttpGet(String url, CloudCallBeak<String> cloudCallBeak) { |
| | | this.requestHttpServer(url, cloudCallBeak); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 请求服务器的方法 |
| | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | HdlLogLogic.print("http->发送->" + fullUrl + "\r\n" + json, isAddToMemory); |
| | | // 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 |
| | |
| | | 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, isAddToMemory); |
| | | // HdlLogLogic.print("http---回复---" + response.request().url() + "\r\n" + s, isAddToMemory); |
| | | baseSuccessCallBeak.onSuccess(httpResponsePack); |
| | | |
| | | } else { |
| | |
| | | call.enqueue(new Callback() {//异步 |
| | | @Override |
| | | public void onFailure(@NonNull Call call, @NonNull IOException e) { |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), isAddToMemory); |
| | | // HdlLogLogic.print("http---回复---" + "\r\n" + e.getMessage(), isAddToMemory); |
| | | } |
| | | |
| | | @Override |
| | |
| | | String s = Objects.requireNonNull(response.body()).string(); |
| | | HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class); |
| | | baseSuccessCallBeak.onSuccess(httpResponsePack); |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + s, isAddToMemory); |
| | | // HdlLogLogic.print("http---回复---" + "\r\n" + s, isAddToMemory); |
| | | } else { |
| | | baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message())); |
| | | } |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage())); |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), isAddToMemory); |
| | | // HdlLogLogic.print("http---回复---" + "\r\n" + e.getMessage(), isAddToMemory); |
| | | } |
| | | |
| | | } |