java通过api查询a股实时行情 | java代码速记 | java 技术论坛-大发黄金版app下载
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={"trace" : "java_http_test1","data" : {"code" : "600519.sh","kline_type" : 1,"kline_timestamp_end" : 0,"query_kline_num" : 2,"adjust_type": 0}}";
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
}
}