利用Java怎么实时获取基金收益项目(java,开发技术)

时间:2024-05-08 00:37:14 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :
/***httClient请求GET*获取基金网数据1*/publicstaticJSONArraytestDepartmentList1(Stringcode){IntegerpageIndex=1;IntegerpageSize=20;StringstartTime="2018-1-1";StringendTime="2020-4-15";Stringreferer="http://fundf10.eastmoney.com/f10/jjjz_"+code+".html";longtime=System.currentTimeMillis();Stringurl="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);HttpRequestrequest=HttpUtil.createGet(url);request.header("Referer",referer);Stringstr=request.execute().body();//获取str的长度System.out.println("str="+str);intlength=str.length();System.out.println("length="+length);//indexOf返回某个指定的字符串值在字符串中首次出现的位置intindexStart=str.indexOf("(");System.out.println(indexStart);//截取字符串str=str.substring(indexStart+9,length-90);System.out.println(str);//转换为Obj类型JSONObjectjsonObject=JSON.parseObject(str);System.out.println(jsonObject);//获取数组JSONArrayjsonArray=jsonObject.getJSONArray("LSJZList");//计算数组的长度intsize=jsonArray.size();System.out.println(size);returnjsonArray;}

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

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

/***httClient请求GET*获取基金网数据2*/@TestpublicstaticStringtestDepartmentList2(Stringcode){//数据链接Stringreferer="http://so.eastmoney.com/web/s?keyword="+code+"";longtime=System.currentTimeMillis();Stringurl="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请求HttpRequestrequest=HttpUtil.createGet(url);request.header("Referer",referer);Stringstr=request.execute().body();//获取str的长度System.out.println("str="+str);intlength=str.length();System.out.println("length="+length);//indexOf返回某个指定的字符串值在字符串中首次出现的位置inti=str.indexOf("(");System.out.println(i);//截取字符串str=str.substring(i+55,length-3);System.out.println(str);//转换为Obj类型JSONObjectjsonObject=JSON.parseObject(str);System.out.println(jsonObject);StringfundName=jsonObject.getString("f58");returnfundName;}

java实时获取基金收益

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

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

@OverridepublicList<FundHistory>query(StringfundCode){List<FundHistory>query=fundHistoryDao.query(fundCode);if(query.size()==0){JSONArrayjsonArray=testDepartmentList1(fundCode);System.out.println(jsonArray);//计算数组的长度intsize=jsonArray.size();System.out.println(size);//for循环遍历for(intj=0;j<size;j++){JSONObjectjsonObject1=jsonArray.getJSONObject(j);//获取净值日期Stringdate=jsonObject1.getString("FSRQ");//获取单位净值Doubleunit=jsonObject1.getDouble("DWJZ");//获取累积净值DoubleAccumulates=jsonObject1.getDouble("LJJZ");//获取日增长率StringgrowthRate=jsonObject1.getString("JZZZL");//创建时间DateTimedateTime=newDateTime();//获取创建时间Stringdatetime=String.valueOf(dateTime);FundHistoryfundHistory=newFundHistory();fundHistory.setFundCode(fundCode);fundHistory.setDate(date);fundHistory.setUnit(unit);fundHistory.setAccumulates(Accumulates);fundHistory.setGrowthRate(growthRate);fundHistory.setCreateTime(datetime);fundHistoryDao.saveFundHistory(fundHistory);}FundHistoryfundHistory=newFundHistory();fundHistory.setFundCode(fundCode);//获取基金名称StringfundName=testDepartmentList2(fundCode);fundHistory.setFundName(fundName);fundHistoryDao.updateFundHistory(fundHistory);List<FundHistory>query2=fundHistoryDao.query(fundCode);FundHistoryfundHistory1=query2.get(0);fundDao.saveFund2(fundHistory1);returnquery2;}returnquery;}

controller层

/***基金页面数据交互*@param*@return*/@RequestMapping("/enquiryfund")@ResponseBodypublicResultenquiryfund(StringfundCode,StringfundName){Resultresult=newResult<>();if(fundCode!=""){List<FundHistory>query=fundHistoryService.query(fundCode);if(query==null){List<FundHistory>query2=fundHistoryService.query(fundCode);result.setData(query2);returnresult;}result.setData(query);returnresult;}elseif(fundName!=""){List<FundHistory>fundHistories=fundHistoryService.query2(fundName);result.setData(fundHistories);returnresult;}returnresult;}

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

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

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

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

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

/***httClient请求GET*获取基金网数据1*/publicstaticJSONArraytestDepartmentList1(Stringcode){IntegerpageIndex=1;IntegerpageSize=20;StringstartTime="2018-1-1";StringendTime="2020-4-15";Stringreferer="http://fundf10.eastmoney.com/f10/jjjz_"+code+".html";longtime=System.currentTimeMillis();Stringurl="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);HttpRequestrequest=HttpUtil.createGet(url);request.header("Referer",referer);Stringstr=request.execute().body();//获取str的长度System.out.println("str="+str);intlength=str.length();System.out.println("length="+length);//indexOf返回某个指定的字符串值在字符串中首次出现的位置intindexStart=str.indexOf("(");System.out.println(indexStart);//截取字符串str=str.substring(indexStart+9,length-90);System.out.println(str);//转换为Obj类型JSONObjectjsonObject=JSON.parseObject(str);System.out.println(jsonObject);//获取数组JSONArrayjsonArray=jsonObject.getJSONArray("LSJZList");//计算数组的长度intsize=jsonArray.size();System.out.println(size);returnjsonArray;}
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:利用Java怎么实时获取基金收益项目的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:使用ElementUI怎么实现一个table嵌套功能下一篇:

9 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18