| | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.hdlhttp.HxHttp; |
| | | import com.hdl.hdlhttp.HxHttpBuilder; |
| | | 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.response.HDLResponse; |
| | | import com.hdl.linkpm.sdk.user.bean.HDLLoginBean; |
| | | import com.hdl.linkpm.sdk.user.callback.ILoginCallBack; |
| | | import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; |
| | | 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.HdlLogLogic; |
| | | import com.hdl.photovoltaic.utils.HDLMD5Utils; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | import io.reactivex.rxjava3.disposables.Disposable; |
| | | import okhttp3.Call; |
| | | import okhttp3.Callback; |
| | | import okhttp3.FormBody; |
| | | import okhttp3.Headers; |
| | | import okhttp3.MediaType; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器的方法 |
| | | * 请求服务器 |
| | | * 底层方法,引用库的方法 |
| | | * |
| | | * @param api 请求接口 |
| | | * @param body 请求参数(appKey,timestamp,sign这三个参数内部会自动添加)) |
| | | * @param callBack 回调 |
| | | * @return - |
| | | */ |
| | | private Disposable request(String api, String 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) |
| | | .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); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, body, e); |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | 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); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 请求服务器 |
| | | * |
| | | * @param api 请求接口 |
| | | * @param body 请求参数(json) |
| | | * @param callBack 回调 |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 请求服务器的方法(已弃用) |
| | | * |
| | | * @param requestUrl 请求接口 |
| | | * @param json 请求数据 |
| | | * @param isBasicService 是否是基础服务的接口(基础服务的接口需要 appKey,timestamp,sign这三个参数,当为true时,内部会自动添加) |
| | | * @param isExecute 是否是同步(true=同步,false=异步) |
| | | */ |
| | | public void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { |
| | | @Deprecated |
| | | private void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { |
| | | String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl; |
| | | this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器的方法(目前只用在获取获取账号区域信息) |
| | | * 请求服务器的方法(已弃用) |
| | | * 目前只用在获取获取账号区域信息 |
| | | * |
| | | * @param fullUrl 绝对地址(地址+接口) |
| | | * @param json 请求数据 |
| | | * @param isBasicService 是否是基础服务的接口(基础服务的接口需要 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); |
| | | @Deprecated |
| | | private void requestFullHttp(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { |
| | | this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param isBasicService 是否是基础服务的接口(基础服务的接口需要 appKey,timestamp,sign这三个参数,当为true时,内部会自动添加) |
| | | * @param isExecute 是否是同步(true=同步,false=异步) |
| | | */ |
| | | private void requestHttps(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { |
| | | private void requestHttpServer(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); |
| | | 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() |
| | | .url(fullUrl)//请求的url// |
| | | .url(fullUrl)//请求的url |
| | | .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()) |
| | | .post(requestBody) |
| | | .build(); |
| | |
| | | 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); |
| | | baseSuccessCallBeak.onSuccess(httpResponsePack); |
| | | HdlLogLogic.print("http->回复->", response.request().url() + "\r\n" + s); |
| | | |
| | | } else { |
| | | //throw new IOException("Unexpected code " + response); |
| | | baseSuccessCallBeak.onFailure(new Exception()); |
| | | baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message())); |
| | | } |
| | | } else { |
| | | |
| | |
| | | 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()); |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), false); |
| | | } |
| | | |
| | | @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); |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + s, false); |
| | | } else { |
| | | baseSuccessCallBeak.onFailure(new Exception()); |
| | | baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message())); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } catch (Exception e) { |
| | | baseSuccessCallBeak.onFailure(e); |
| | | HdlLogLogic.print("http->回复->", "\r\n" + e.getMessage()); |
| | | baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage())); |
| | | HdlLogLogic.print("http->回复->" + "\r\n" + e.getMessage(), false); |
| | | } |
| | | |
| | | } |