| | |
| | | import com.nq.pojo.StockRealTimeBean; |
| | | import com.nq.utils.PropertiesUtil; |
| | | import com.nq.utils.stock.sina.StockApi; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.http.util.TextUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | |
| | | } |
| | | if(stockRealTimeBean == null){ |
| | | String s = doPost(stock.getStockCode()); |
| | | Map<String, Object> stringObjectMap = jsonToMap(s); |
| | | stockRealTimeBean = new StockRealTimeBean(); |
| | | stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString()); |
| | | stockRealTimeBean.setLast(stringObjectMap.get("Last").toString()); |
| | | stockRealTimeBean.setHigh(stringObjectMap.get("High").toString()); |
| | | stockRealTimeBean.setLow(stringObjectMap.get("Low").toString()); |
| | | stockRealTimeBean.setBid(stringObjectMap.get("Id").toString()); |
| | | stockRealTimeBean.setPc(stringObjectMap.get("PrevClose").toString()); |
| | | stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString()); |
| | | if (StringUtils.isBlank(s)) { |
| | | stockRealTimeBean = buildFallbackRealTime(stock); |
| | | } else { |
| | | try { |
| | | Map<String, Object> stringObjectMap = jsonToMap(s); |
| | | stockRealTimeBean = new StockRealTimeBean(); |
| | | stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString()); |
| | | stockRealTimeBean.setLast(stringObjectMap.get("Last").toString()); |
| | | stockRealTimeBean.setHigh(stringObjectMap.get("High").toString()); |
| | | stockRealTimeBean.setLow(stringObjectMap.get("Low").toString()); |
| | | stockRealTimeBean.setBid(stringObjectMap.get("Id").toString()); |
| | | stockRealTimeBean.setPc(stringObjectMap.get("PrevClose").toString()); |
| | | stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString()); |
| | | stockRealTimeBean.setPid(stock.getStockCode()); |
| | | } catch (Exception e) { |
| | | log.warn("解析实时行情失败, code={}, err={}", stock.getStockCode(), e.getMessage()); |
| | | stockRealTimeBean = buildFallbackRealTime(stock); |
| | | } |
| | | } |
| | | } |
| | | stockRealTimeBean.setPcp(stockRealTimeBean.getPcp().replace("%","")); |
| | | if (stockRealTimeBean.getPcp() != null) { |
| | | stockRealTimeBean.setPcp(stockRealTimeBean.getPcp().replace("%","")); |
| | | } |
| | | return stockRealTimeBean; |
| | | |
| | | } |
| | | |
| | | private static StockRealTimeBean buildFallbackRealTime(Stock stock) { |
| | | StockRealTimeBean bean = new StockRealTimeBean(); |
| | | bean.setPid(stock.getStockCode()); |
| | | bean.setLast("0"); |
| | | bean.setHigh("0"); |
| | | bean.setLow("0"); |
| | | bean.setBid("0"); |
| | | bean.setAsk("0"); |
| | | bean.setPc("0"); |
| | | bean.setPcp("0"); |
| | | return bean; |
| | | } |
| | | |
| | | public static Map<String, Object> jsonToMap(String json) { |
| | |
| | | private static String executePostRequest(String pid) { |
| | | String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + |
| | | "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY"); |
| | | |
| | | HttpURLConnection connection = null; |
| | | try { |
| | | URL url = new URL(apiUrl); |
| | | HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| | | connection = (HttpURLConnection) url.openConnection(); |
| | | |
| | | connection.setRequestMethod("POST"); |
| | | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | connection.setDoOutput(true); |
| | | connection.setConnectTimeout(5000); |
| | | connection.setReadTimeout(10000); |
| | | connection.setConnectTimeout(3000); |
| | | connection.setReadTimeout(5000); |
| | | |
| | | String postData = "pid=" + pid; |
| | | |
| | |
| | | os.write(input, 0, input.length); |
| | | } |
| | | |
| | | // 读取响应 |
| | | try (BufferedReader in = new BufferedReader( |
| | | new InputStreamReader(connection.getInputStream()))) { |
| | | new InputStreamReader(connection.getInputStream(), "utf-8"))) { |
| | | |
| | | StringBuilder response = new StringBuilder(); |
| | | String inputLine; |
| | |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; // 返回null表示请求失败 |
| | | log.warn("拉取实时行情失败 pid={}, err={}", pid, e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | if (connection != null) { |
| | | connection.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | |