广告
返回顶部
首页 > 资讯 > 精选 >利用Java怎么实时获取基金收益项目
  • 846
分享到

利用Java怎么实时获取基金收益项目

2023-06-14 05:06:38 846人浏览 八月长安
摘要

本篇文章给大家分享的是有关利用Java怎么实时获取基金收益项目,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。  public static&nbs

本篇文章给大家分享的是有关利用Java怎么实时获取基金收益项目,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

  public static JSONArray testDepartmentList1(String code){    Integer pageIndex = 1;    Integer pageSize=20;    String startTime="2018-1-1";    String endTime = "2020-4-15";    String referer = "Http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";    long time = System.currentTimeMillis();    String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=Jquery18306596328894644803_1571038362181&" +        "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";    url = String.fORMat(url,code,pageIndex,pageSize,startTime,endTime,time);    System.out.println("url= " + url);    System.out.println(url);    HttpRequest request = HttpUtil.createGet(url);    request.header("Referer", referer);    String str = request.execute().body();    //获取str的长度    System.out.println("str=" + str);    int length = str.length();    System.out.println("length=" + length);    //indexOf返回某个指定的字符串值在字符串中首次出现的位置    int indexStart = str.indexOf("(");    System.out.println(indexStart);    //截取字符串    str = str.substring(indexStart + 9, length - 90);    System.out.println(str);    //转换为Obj类型    jsONObject jsonObject = JSON.parseObject(str);    System.out.println(jsonObject);    //获取数组    JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");    //计算数组的长度    int size = jsonArray.size();    System.out.println(size);     return jsonArray;  }

通过基金编码查询基金名称

(由于基金网url里面的信息只有基金编号跟涨跌幅日期等 没有基金名称 我们通过基金网的查询功能自行填充基金编码进行查询)

  @Test  public static String testDepartmentList2(String code) {    //数据链接    String referer = "http://so.eastmoney.com/WEB/s?keyWord="+code+"";     long time = System.currentTimeMillis();     String url = "http://push3.eastmoney.com/api/Qt/stock/get?ut=fa5fd1943c7b386f172d6893Dbfba10b&fltt" +        "=2&fields=f59,f169,f170,f161,f163,f171,f126,f168,f164,f78,f162,f43,f46,f44,f45,f60,f47," +        "f48,f49,f84,f116,f55,f92,f71,f50,f167,f117,f85,f84,f58,f57,f86,f172,f108,f118,f107,f164," +        "f177&invt=2&secid=0."+code+"&cb=jQuery1124006112441213993569_1587006450385&_=1587006450403";    url = String.format(url,code);    System.out.println("请求url:" + url);    //http请求    HttpRequest request = HttpUtil.createGet(url);     request.header("Referer", referer);    String str = request.execute().body();    //获取str的长度    System.out.println("str=" + str);    int length = str.length();    System.out.println("length=" + length);    //indexOf返回某个指定的字符串值在字符串中首次出现的位置    int i = str.indexOf("(");    System.out.println(i);    //截取字符串    str = str.substring(i + 55, length - 3);    System.out.println(str);    //转换为Obj类型    JSONObject jsonObject = JSON.parseObject(str);    System.out.println(jsonObject);    String fundName = jsonObject.getString("f58");    return fundName;  }

java实时获取基金收益

业务层实现:(主要功能:用户输入基金编号查询某个基金时如果数据库没有,自动从天天基金网爬取数据存储到数据库并显示到页面上)

显示的数据分别有:基金编号 基金日期 基金名称 实际价格 每日涨跌幅

@Override  public List<FundHistory> query(String fundCode) {    List<FundHistory> query = fundHistoryDao.query(fundCode);    if (query.size()==0) {      JSONArray jsonArray = testDepartmentList1(fundCode);      System.out.println(jsonArray);      //计算数组的长度      int size = jsonArray.size();      System.out.println(size);      //for循环遍历      for (int j = 0; j < size; j++) {        JSONObject jsonObject1 = jsonArray.getJSONObject(j);        //获取净值日期        String date = jsonObject1.getString("FSRQ");        //获取单位净值        Double unit = jsonObject1.getDouble("DWJZ");        //获取累积净值        Double Accumulates = jsonObject1.getDouble("LJJZ");        //获取日增长率        String growthRate = jsonObject1.getString("JZZZL");        //创建时间        DateTime dateTime = new DateTime();        //获取创建时间        String datetime = String.valueOf(dateTime);        FundHistory fundHistory = new FundHistory();        fundHistory.setFundCode(fundCode);        fundHistory.setDate(date);        fundHistory.setUnit(unit);        fundHistory.setAccumulates(Accumulates);        fundHistory.setGrowthRate(growthRate);        fundHistory.setCreateTime(datetime);        fundHistoryDao.saveFundHistory(fundHistory);      }      FundHistory fundHistory = new FundHistory();      fundHistory.setFundCode(fundCode);      //获取基金名称      String fundName = testDepartmentList2(fundCode);      fundHistory.setFundName(fundName);      fundHistoryDao.updateFundHistory(fundHistory);      List<FundHistory> query2 = fundHistoryDao.query(fundCode);      FundHistory fundHistory1 = query2.get(0);      fundDao.saveFund2(fundHistory1);      return query2;    }    return query;  }

controller层

   @RequestMapping("/enquiryfund")  @ResponseBody  public Result enquiryfund(String fundCode,String fundName){    Result result = new Result<>();    if (fundCode!=""){      List<FundHistory> query = fundHistoryService.query(fundCode);      if (query==null){        List<FundHistory> query2 = fundHistoryService.query(fundCode);        result.setData(query2);        return result;      }      result.setData(query);      return result;    }else if (fundName!=""){      List<FundHistory> fundHistories = fundHistoryService.query2(fundName);      result.setData(fundHistories);      return result;    }    return result;  }

java实时获取基金收益项目运行效果如图:

利用Java怎么实时获取基金收益项目

利用Java怎么实时获取基金收益项目

利用Java怎么实时获取基金收益项目

(根据基金编号进行查询基金 如果数据库没有则自动从天天基金网拉取数据并显示到页面上 共拉取20条历史数据)

   public static JSONArray testDepartmentList1(String code){  Integer pageIndex = 1;  Integer pageSize=20;  String startTime="2018-1-1";  String endTime = "2020-4-15";  String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";  long time = System.currentTimeMillis();  String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18306596328894644803_1571038362181&" +      "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";  url = String.format(url,code,pageIndex,pageSize,startTime,endTime,time);  System.out.println("url= " + url);  System.out.println(url);  HttpRequest request = HttpUtil.createGet(url);  request.header("Referer", referer);  String str = request.execute().body();  //获取str的长度System.out.println("str=" + str);  int length = str.length();  System.out.println("length=" + length);  //indexOf返回某个指定的字符串值在字符串中首次出现的位置int indexStart = str.indexOf("(");  System.out.println(indexStart);  //截取字符串str = str.substring(indexStart + 9, length - 90);  System.out.println(str);  //转换为Obj类型JSONObject jsonObject = JSON.parseObject(str);  System.out.println(jsonObject);  //获取数组JSONArray jsonArray = jsonObject.getJSONArray("LSJZList");  //计算数组的长度int size = jsonArray.size();  System.out.println(size);   return jsonArray;}

以上就是利用Java怎么实时获取基金收益项目,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。

--结束END--

本文标题: 利用Java怎么实时获取基金收益项目

本文链接: https://www.lsjlt.com/news/268029.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作