java调用tushare pro的http方法教程,附带源码

tushare 是一个股票API平台,数据全面准确及时。

1.注册用户,注册用户使用链接:https://tushare.pro/register?reg=266769

2.登录用户

3.进入个人主页,获取到token

java调用tushare pro的http方法教程,附带源码

4.根据api测试接口

api网址:https://tushare.pro/document/2

注册用户使用链接:https://tushare.pro/register?reg=266769

5.源代码

    public static void main(String[] args) throws Exception {
        //请求URL
        String execurl="http://api.tushare.pro";
        String token="你的token";
        Getstock_company(execurl,token);
    }
    
    private static void GetVersionInfo(String execurl,String token) throws Exception {
        Map<String, String> paramMap=new HashMap<>();
        paramMap.put("api_name", "stock_basic");
        paramMap.put("token", token);
        Map<String, String> paramsdate=new HashMap<>();
        paramMap.put("params", JSONObject.fromObject(paramsdate).toString());
        paramMap.put("fields", "ts_code,symbol,name,area,industry,list_date,is_hs");
        String body = NetHttpUtils.SentPostBody(execurl, paramMap);
        System.out.println(body);
        JSONObject object=JSONObject.fromObject(body);
        JSONArray items = object.getJSONObject("data").getJSONArray("items");
        for (int i = 0; i < items.size(); i++) {
            JSONArray o=JSONArray.fromObject(items.get(i));
            System.out.println(o.get(2));
        }
    }

    
    public static String SentPostBody(String execurl,Map<String, String> paramMap) {
        OutputStreamWriter out = null;
        InputStream is = null;
        try {
            URL url = new URL(execurl);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            out = new OutputStreamWriter(connection.getOutputStream()); // utf-8编码
            out.append(JSONObject.fromObject(paramMap).toString());
            out.flush();
            out.close();

            // 读取响应
            is = connection.getInputStream();
            int length = (int) connection.getContentLength();
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                String result = new String(data, "utf-8"); 
                return result;
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }