try { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 转换为指定格式的字符串 String dateString = currentDate.format(formatter); // 构建URL对象 URL url = new URL(API_BASE_URL + "?begin=" + dateString + "&Methodname=" + URLEncoder.encode(str, "UTF-8")); // 创建HttpURLConnection对象 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 设置Basic认证信息 String auth = API_USERNAME + ":" + API_PASSWORD; byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8); String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(authBytes); connection.setRequestProperty("Authorization", authHeaderValue); // 设置字符编码为UTF-8 connection.setRequestProperty("Accept-Charset", "UTF-8"); // 发送请求 int responseCode = connection.getResponseCode(); // 读取响应数据 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); StringBuilder responseBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseBuilder.append(line); } String response = responseBuilder.toString(); // 关闭连接 connection.disconnect(); // 返回响应数据 ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response); String ls = rootNode.get("data").get(0).toString(); return ls; } catch (Exception e) { e.printStackTrace(); return "Error: " + e.getMessage(); }其中ls的值是"{\"ls\":\"603\"}",修改一下,ls只需要”603“
时间: 2024-03-27 07:40:55 浏览: 79
在获取到 `ls` 的值时,可以使用 `JsonNode` 的 `get()` 方法获取到 `ls` 字段对应的值,然后使用 `String` 的 `replaceAll()` 方法去除多余的字符,具体修改如下:
```
try {
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 定义日期格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 转换为指定格式的字符串
String dateString = currentDate.format(formatter);
// 构建URL对象
URL url = new URL(API_BASE_URL + "?begin=" + dateString + "&Methodname=" + URLEncoder.encode(str, "UTF-8"));
// 创建HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 设置Basic认证信息
String auth = API_USERNAME + ":" + API_PASSWORD;
byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(authBytes);
connection.setRequestProperty("Authorization", authHeaderValue);
// 设置字符编码为UTF-8
connection.setRequestProperty("Accept-Charset", "UTF-8");
// 发送请求
int responseCode = connection.getResponseCode();
// 读取响应数据
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
StringBuilder responseBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseBuilder.append(line);
}
String response = responseBuilder.toString();
// 关闭连接
connection.disconnect();
// 返回响应数据
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(response);
String ls = rootNode.get("data").get(0).get("ls").asText().replaceAll("[^\u4e00-\u9fa5^\\d]", "");
return ls;
} catch (Exception e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
```
在上述代码中,使用了 `JsonNode` 的 `asText()` 方法将 `ls` 的值转化为字符串类型,然后使用 `replaceAll()` 方法去除多余字符,只保留数字和中文。
阅读全文