Browse Source

处理收费站接口超时

develop
王兴琳 8 months ago
parent
commit
49635b01b7
  1. 55
      zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

55
zc-business/src/main/java/com/zc/business/service/impl/DcTrafficStatisticsServiceImpl.java

@ -187,7 +187,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
*/ */
@Override @Override
public JSONArray vehiclesInTransit() throws HttpException, IOException { public JSONArray vehiclesInTransit() throws HttpException, IOException {
OkHttp okHttp = new OkHttp(); OkHttp okHttp = new OkHttp(20);
RequestParams requestParams = new RequestParams(); RequestParams requestParams = new RequestParams();
@ -678,7 +678,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
*/ */
@Override @Override
public List<Map<String, String>> trafficFlowAtTollStationEntrance(String startDate, String endDate, String stationType) throws HttpException, IOException { public List<Map<String, String>> trafficFlowAtTollStationEntrance(String startDate, String endDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp(); OkHttp okHttp = new OkHttp(20);
RequestParams requestParams = new RequestParams(); RequestParams requestParams = new RequestParams();
@ -697,26 +697,27 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken()); headers.put("Authorization", getAccessToken());
try {
Response response // 请求响应 Response response = okHttp
= okHttp
.headers(headers) .headers(headers)
.url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue()) // 请求地址 .url(baseUrl + UniversalEnum.EACH_TOLL_STATION_ENTRANCE_BY_TYPE_OF_HOURLY_TRAFFIC_FLOW.getValue())
.data(requestParams) // 请求参数 .data(requestParams)
.post(); // 请求方法 .post();
// 确保响应成功
if (!response.isSuccessful()) {
throw new IOException("请求不成功,HTTP代码:" + response.code());
}
ResponseBody body = response.body(); ResponseBody body = response.body();
if (body != null) { if (body != null) {
String jsonString = body.string(); String jsonString = body.string();
System.out.println(jsonString);
if (JSON.isValidArray(jsonString)) { if (JSON.isValidArray(jsonString)) {
JSONArray jsonArray = JSON.parseArray(jsonString); JSONArray jsonArray = JSON.parseArray(jsonString);
Map<String, Integer> sumByName = new LinkedHashMap<>(); Map<String, Integer> sumByName = new LinkedHashMap<>();
List<Map<String, String>> list = new ArrayList(); List<Map<String, String>> list = new ArrayList();
for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object for (Object item : jsonArray) { // 这里做了微调,直接遍历jsonArray的Object
JSONObject jsonObject = (JSONObject) item; JSONObject jsonObject = (JSONObject) item;
// 获取当前时间 // 获取当前时间
LocalTime now = LocalTime.now(); LocalTime now = LocalTime.now();
// 获取当前小时数(24小时制) // 获取当前小时数(24小时制)
@ -739,11 +740,17 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
} }
} }
return new ArrayList(); return new ArrayList();
} catch (IOException e) {
// 处理异常
e.printStackTrace();
return new ArrayList();
}
} }
@Override @Override
public JSONArray trafficFlowAtTollStationEntranceHour(String startDate, String endDate, String stationType) throws HttpException, IOException { public JSONArray trafficFlowAtTollStationEntranceHour(String startDate, String endDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp(); OkHttp okHttp = new OkHttp(15);
RequestParams requestParams = new RequestParams(); RequestParams requestParams = new RequestParams();
@ -793,13 +800,14 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
LocalDate oneYearAgo = currentDate.minusYears(UniversalEnum.ONE.getNumber()); LocalDate oneYearAgo = currentDate.minusYears(UniversalEnum.ONE.getNumber());
String lastYear = oneYearAgo.format(formatter); String lastYear = oneYearAgo.format(formatter);
OkHttp okHttp = new OkHttp(); OkHttp okHttp = new OkHttp(20);
RequestParams requestParams = new RequestParams(); RequestParams requestParams = new RequestParams();
requestParams.put("sysid", sysid); requestParams.put("sysid", sysid);
//求情地址 startDate 时间 //求情地址 startDate 时间
JSONArray body = getResponseBody(nowYear, requestParams, okHttp); JSONArray body = getResponseBody(nowYear, requestParams, okHttp);
JSONArray bodylast = getResponseBody(lastYear, requestParams, okHttp); JSONArray bodylast = getResponseBody(lastYear, requestParams, okHttp);
Map<String, List<Map<String, Object>>> map = new HashMap<>(); Map<String, List<Map<String, Object>>> map = new HashMap<>();
if (body != null) { if (body != null) {
@ -888,7 +896,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
requestParams.put("parameters", parameters.toJSONString()); requestParams.put("parameters", parameters.toJSONString());
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put("Authorization", getAccessToken()); headers.put("Authorization", getAccessToken());
try {
Response response // 请求响应 Response response // 请求响应
= okHttp = okHttp
.headers(headers) .headers(headers)
@ -903,9 +911,15 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
} else { } else {
return new JSONArray(); return new JSONArray();
} }
}else {
return new JSONArray();
} }
} catch (IOException e) {
// 记录日志或处理异常
e.printStackTrace();
return new JSONArray(); return new JSONArray();
} }
}
//格式化桩号 //格式化桩号
private static String extract(String input) { private static String extract(String input) {
@ -1093,9 +1107,9 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue()); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(UniversalEnum.TIME_FORMAT_YEARS_MONTH_DAY.getValue());
LocalDate currentDate = LocalDate.now(); LocalDate currentDate = LocalDate.now();
String startDate = currentDate.format(formatter); String startDate = currentDate.format(formatter);
try {
Response response = getResponseTrafficFlowAtToll(startDate, UniversalEnum.ONE.getValue()); Response response = getResponseTrafficFlowAtToll(startDate, UniversalEnum.ONE.getValue());
Response responseTwo = getResponseTrafficFlowAtToll(startDate, UniversalEnum.TWO.getValue()); Response responseTwo = getResponseTrafficFlowAtToll(startDate, UniversalEnum.TWO.getValue());
ResponseBody body = response.body(); ResponseBody body = response.body();
ResponseBody bodyTwo = responseTwo.body(); ResponseBody bodyTwo = responseTwo.body();
Integer totalFlow1 = UniversalEnum.ZERO.getNumber(); Integer totalFlow1 = UniversalEnum.ZERO.getNumber();
@ -1120,11 +1134,20 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi
} }
} }
} }
return totalFlow1 + totalFlow2; return totalFlow1 + totalFlow2;
} }
catch (IOException e) {
// 处理异常
e.printStackTrace();
return UniversalEnum.ZERO.getNumber();
}
}
private Response getResponseTrafficFlowAtToll(String startDate, String stationType) throws HttpException, IOException { private Response getResponseTrafficFlowAtToll(String startDate, String stationType) throws HttpException, IOException {
OkHttp okHttp = new OkHttp(); OkHttp okHttp = new OkHttp(20);
RequestParams requestParams = new RequestParams(); RequestParams requestParams = new RequestParams();

Loading…
Cancel
Save