| | |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.hdlhttp.utils.GsonConvert; |
| | | import com.hdl.photovoltaic.bean.ResponsePack; |
| | | import com.hdl.photovoltaic.config.ConfigManagement; |
| | | 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.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.utils.HDLMD5Utils; |
| | | |
| | | import java.io.IOException; |
| | |
| | | * @param isExecute 是否是同步(true=同步,false=异步) |
| | | */ |
| | | public void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) { |
| | | String fullUrl = ConfigManagement.getUserRegionUrl() + requestUrl; |
| | | String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl; |
| | | this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | } |
| | | |
| | |
| | | this.requestHttps(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 请求服务器的方法 |
| | | * |
| | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | |
| | | HdlLogLogic.print("http->发送->", fullUrl + "\r\n" + json); |
| | | 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// |
| | | .addHeader("Authorization", "accessToken") |
| | | .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()) |
| | | .post(requestBody) |
| | | .build(); |
| | | if (isExecute) { |
| | | Response response = okHttpClient.newCall(request).execute();//同步 |
| | | if (response.isSuccessful()) { |
| | | String s = Objects.requireNonNull(response.body()).string(); |
| | | ResponsePack responsePack = new Gson().fromJson(s, ResponsePack.class); |
| | | baseSuccessCallBeak.onSuccess(responsePack); |
| | | HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class); |
| | | baseSuccessCallBeak.onSuccess(httpResponsePack); |
| | | HdlLogLogic.print("http->回复->", response.request().url() + "\r\n" + s); |
| | | } else { |
| | | //throw new IOException("Unexpected code " + response); |
| | | baseSuccessCallBeak.onFailure(new Exception()); |
| | |
| | | @Override |
| | | public void onFailure(@NonNull Call call, @NonNull IOException e) { |
| | | System.out.println("失败" + e.toString()); |
| | | HdlLogLogic.print("http->回复->", "\r\n" + e.getMessage()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | System.out.println("url连接信息" + response.code()); |
| | | if (response.code() == 200) { |
| | | String s = Objects.requireNonNull(response.body()).string(); |
| | | ResponsePack responsePack = new Gson().fromJson(s, ResponsePack.class); |
| | | baseSuccessCallBeak.onSuccess(responsePack); |
| | | HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class); |
| | | baseSuccessCallBeak.onSuccess(httpResponsePack); |
| | | HdlLogLogic.print("http->回复->", "\r\n" + s); |
| | | } else { |
| | | baseSuccessCallBeak.onFailure(new Exception()); |
| | | } |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | baseSuccessCallBeak.onFailure(e); |
| | | HdlLogLogic.print("http->回复->", "\r\n" + e.getMessage()); |
| | | } |
| | | |
| | | } |
| | | |
| | | }).start(); |
| | | } |
| | | |
| | |
| | | */ |
| | | private String getJson(String json) { |
| | | final String timestamp = String.valueOf(System.currentTimeMillis()); |
| | | final String appKey = ConfigManagement.getAppKey(); |
| | | final String appSecret = ConfigManagement.getAppSecret(); |
| | | final String appKey = AppConfigManage.getAppKey(); |
| | | final String appSecret = AppConfigManage.getAppSecret(); |
| | | JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class); |
| | | if (jsonObject == null) { |
| | | jsonObject = new JsonObject(); |