Java通过API查询A股实时行情

HTTP请求

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 示例代码:查询A股上市公司茅台(股票代码:600519.SH)的实时股价
 * 
 * API注册地址:https://alltick.co/register
 * API文档:https://apis.alltick.co/
 */

public class HttpJavaExample {

    public static void main(String[] args) {

        try {

            /*
            Encode the following JSON with URL encoding and copy it into the "query" field of the HTTP query string.
            {"trace" : "java_http_test1","data" : {"code" : "600519.SH","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}
            */
            String url = "http://quote.aatest.online/quote-stock-b-api/kline?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806&query=%7B%22trace%22%20%3A%20%22java_http_test1%22%2C%22data%22%20%3A%20%7B%22code%22%20%3A%20%22600519.SH%22%2C%22kline_type%22%20%3A%201%2C%22kline_timestamp_end%22%20%3A%200%2C%22query_kline_num%22%20%3A%202%2C%22adjust_type%22%3A%200%7D%7D";

            URL obj = new URL(url);

            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            con.setRequestMethod("GET");

            int responseCode = con.getResponseCode();

            System.out.println("Response Code: " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String inputLine;

            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {

                response.append(inputLine);

            }
            in.close();
            System.out.println(response.toString());

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Websocket

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.*;

@ClientEndpoint
public class WebSocketJavaExample {

    private Session session;

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to server");
        this.session = session;
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("Received message: " + message);
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        System.out.println("Disconnected from server");
    }

    @OnError
    public void onError(Throwable throwable) {
        System.err.println("Error: " + throwable.getMessage());
    }

    public void sendMessage(String message) throws Exception {
        this.session.getBasicRemote().sendText(message);
    }

    public static void main(String[] args) throws Exception, URISyntaxException, DeploymentException, IOException, IllegalArgumentException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        WebSocketContainer container = ContainerProvider.getWebSocketContainer();

        URI uri = new URI("wss://quote.aatest.online/quote-stock-b-ws-api?token=e945d7d9-9e6e-4721-922a-7251a9d311d0-1678159756806"); // Replace with your websocket endpoint URL

        WebSocketJavaExample client = new WebSocketJavaExample();
        container.connectToServer(client, uri);

        // Send message to the server to query Moutai (600519.SH)
        client.sendMessage("{\"cmd_id\": 22002, \"seq_id\": 123, \"trace\": \"3baaa938-f92c-4a74-a228-fd49d5e2f8bc-1678419657806\", \"data\": {\"symbol_list\": [{\"code\": \"600519.SH\", \"depth_level\": 5}]}}");

        // Wait for the client to be disconnected from the server (or until the user presses Enter)
        System.in.read(); // Wait for user input before closing the program
    }
}
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!