6 changed files with 144 additions and 12 deletions
-
12pom.xml
-
12src/main/java/com/deepchart/controller/IndexController.java
-
17src/main/java/com/deepchart/entity/StockDailyData.java
-
9src/main/java/com/deepchart/entity/StockInfo.java
-
6src/main/java/com/deepchart/service/impl/IndexServiceImpl.java
-
100src/main/java/com/deepchart/utils/StockDataUtil.java
@ -0,0 +1,17 @@ |
|||||
|
package com.deepchart.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.time.LocalDate; |
||||
|
|
||||
|
/** |
||||
|
* 股票单日行情基本数据实体类 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class StockDailyData { |
||||
|
private LocalDate date; // 交易日日期 |
||||
|
private double openPrice; // 开盘价 |
||||
|
private double closePrice; // 收盘价 |
||||
|
private double lowPrice; // 最低价 |
||||
|
private double highPrice; // 最高价 |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
package com.deepchart.entity; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class StockInfo { |
||||
|
private String market; |
||||
|
private String code; |
||||
|
} |
||||
@ -1,8 +1,14 @@ |
|||||
package com.deepchart.service.impl; |
package com.deepchart.service.impl; |
||||
|
|
||||
import com.deepchart.service.IndexService; |
import com.deepchart.service.IndexService; |
||||
|
import com.deepchart.utils.StockDataUtil; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
import org.springframework.stereotype.Service; |
||||
|
|
||||
@Service |
@Service |
||||
public class IndexServiceImpl implements IndexService { |
public class IndexServiceImpl implements IndexService { |
||||
|
|
||||
|
@Autowired |
||||
|
private StockDataUtil stockDataUtil; |
||||
|
|
||||
} |
} |
||||
@ -0,0 +1,100 @@ |
|||||
|
package com.deepchart.utils; |
||||
|
|
||||
|
import com.deepchart.entity.StockDailyData; |
||||
|
import com.deepchart.entity.StockInfo; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.io.OutputStream; |
||||
|
import java.net.HttpURLConnection; |
||||
|
import java.net.URL; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.time.LocalDate; |
||||
|
import java.time.format.DateTimeFormatter; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
/** |
||||
|
* 股票行情基本数据工具类 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class StockDataUtil { |
||||
|
private static final String STOCK_DATA_PREFIX = "https://api.homilychart.com/link"; |
||||
|
private static final String TOKEN = "8nkj4QBV1RPIb4CzoRTnbZi0+fEeMx8pywnIlrmTxdwROKkuwWqAWu9orpkpeXVqL98DPfeonNYpHv+mucA"; |
||||
|
private static final ObjectMapper objectMapper = new ObjectMapper(); |
||||
|
|
||||
|
public List<StockDailyData> getStockData(StockInfo stock) { |
||||
|
|
||||
|
List<List<String>> kLine20 = List.of(); |
||||
|
|
||||
|
try { |
||||
|
// 1. 构建请求URL |
||||
|
String url = STOCK_DATA_PREFIX + "/api/superBrainData"; |
||||
|
|
||||
|
// 2. 使用Map构造请求体参数 |
||||
|
Map<String, Object> requestBody = new HashMap<>(); |
||||
|
requestBody.put("brainPrivilegeState", 1); |
||||
|
requestBody.put("marketList", "can,usa,hk,vi,sg,th,in,cn,gb,my"); |
||||
|
requestBody.put("market", stock.getMarket()); |
||||
|
requestBody.put("code", stock.getCode()); |
||||
|
|
||||
|
// 3. 将Map转换为JSON字符串 |
||||
|
String jsonInputString = objectMapper.writeValueAsString(requestBody); |
||||
|
|
||||
|
// 4. 创建HTTP连接 |
||||
|
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); |
||||
|
connection.setRequestMethod("POST"); |
||||
|
connection.setRequestProperty("Content-Type", "application/json"); |
||||
|
connection.setRequestProperty("token", TOKEN); |
||||
|
connection.setDoOutput(true); |
||||
|
|
||||
|
// 5. 发送请求数据 |
||||
|
try (OutputStream os = connection.getOutputStream()) { |
||||
|
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); |
||||
|
os.write(input, 0, input.length); |
||||
|
} |
||||
|
|
||||
|
// 6. 解析响应 |
||||
|
if (connection.getResponseCode() == 200) { |
||||
|
try (BufferedReader br = new BufferedReader( |
||||
|
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { |
||||
|
StringBuilder response = new StringBuilder(); |
||||
|
String responseLine; |
||||
|
while ((responseLine = br.readLine()) != null) { |
||||
|
response.append(responseLine.trim()); |
||||
|
} |
||||
|
|
||||
|
// 解析JSON响应 |
||||
|
Map<String, Object> jsonResponse = objectMapper.readValue(response.toString(), Map.class); |
||||
|
Map<String, Object> data = (Map<String, Object>) jsonResponse.get("data"); |
||||
|
Map<String, Object> brain = (Map<String, Object>) data.get("Brain"); |
||||
|
kLine20 = (List<List<String>>) brain.get("KLine20"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 7. 关闭连接 |
||||
|
connection.disconnect(); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
List<StockDailyData> list = new ArrayList<>(); |
||||
|
|
||||
|
for (List<String> v : kLine20) { |
||||
|
StockDailyData s = new StockDailyData(); |
||||
|
s.setDate(LocalDate.parse(v.get(0), DateTimeFormatter.ofPattern("yyyy/MM/dd"))); |
||||
|
s.setOpenPrice(Double.parseDouble(v.get(1))); |
||||
|
s.setClosePrice(Double.parseDouble(v.get(2))); |
||||
|
s.setLowPrice(Double.parseDouble(v.get(3))); |
||||
|
s.setHighPrice(Double.parseDouble(v.get(4))); |
||||
|
list.add(s); |
||||
|
} |
||||
|
|
||||
|
return list; |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue