zoukankan      html  css  js  c++  java
  • 【笔记】获取新浪财经最新的USDT-CNY的汇率

    ---恢复内容开始---

    package util;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.math.BigDecimal;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.log4j.Logger;
    import org.directwebremoting.json.types.JsonArray;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import hry.core.constant.StringConstant;
    import hry.core.quartz.QuartzJob;
    import hry.core.quartz.QuartzManager;
    import hry.core.quartz.ScheduleJob;
    import hry.core.util.date.DateUtil;
    import hry.core.util.sys.ContextUtil;
    import hry.redis.common.utils.RedisService;
    
    
    
    /**
     * 调用汇率
     * 
     * @author Administrator
     * 
     */
    @Service
    @Scope
    public class HuiLvService {
    	private static final Logger log = Logger.getLogger(HuiLvService.class);
    
        /**
         * @param urlAll
         *            :请求接口
         * @param httpArg
         *            :参数
         * @return 返回结果
         */
        public static void getHuilvData() {
        	log.info("-----进入汇率定时器-----");
        	
        	RedisService redisService = (RedisService) ContextUtil.getBean("redisService");
        	JSONArray obj= JSON.parseArray(redisService.get("configCache:financeConfig"));
        	for(Object o:obj){
    			JSONObject	 oo=JSON.parseObject(o.toString());
    			if("ChooseRate".equals(oo.getString("configkey"))){
    				String value =oo.getString("value");
    				if("1".equals(value)){
    					redisService.save("USDCNY", "0");
    					log.info("读取后台手动配置的USDCNY汇率");
    					return;
    				}
    			}
    		}
            BufferedReader reader = null;
            String result = null;
            StringBuffer sbf = new StringBuffer();
            long currentTime=System.currentTimeMillis();
            String httpUrl = "http://hq.sinajs.cn/";
            String httpArg = "rn="+currentTime+"list=fx_susdcny";
            httpUrl = httpUrl + "?" + httpArg;
            try {
                URL url = new URL(httpUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "GBK"));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("
    ");
                }
                reader.close();
                result = sbf.toString();
                String [] arr = result.split(",");
                redisService.save("USDCNY", arr[8]);
                log.info("最新USDCNY汇率:" +new BigDecimal(arr[8]));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
    	 * getHuilvData 定时器
    	 * <p>
    	 * TODO
    	 * </p>
    	 * 
    	 */
    	public static void getHuilvDataJob() {
    		ScheduleJob job = new ScheduleJob();
    		job.setBeanClass("util.HuiLvService");
    		job.setMethodName("getHuilvData");
    //		Object[] object = { 1 };
    //		job.setMethodArgs(object);
    		QuartzManager.addJob("huilvData", job, QuartzJob.class, "0 0/5 * * * ?");//5分钟
    	}
        public static void main(String[] args) {
            String httpUrl = "http://hq.sinajs.cn/";
            String httpArg = "rn=1526351165800list=fx_susdcny";
            long currentTime=System.currentTimeMillis();
            System.out.println(currentTime);
    //        String send = getHuilvData(httpUrl, httpArg);
    //        String send = HttpConnectionUtil.getSend(httpUrl, httpArg);
    //        System.out.println(send);
        }
    }
    

      

    ---恢复内容结束---

  • 相关阅读:
    C#发送邮件
    C# MD5加密
    html实现艺术字
    sql日期转换比较问题
    web 抓取
    NHibernate主要数据操作方法
    写日志
    备忘 sql分页
    自我介绍
    企业级应用和互联网应用的区别
  • 原文地址:https://www.cnblogs.com/mybug/p/9354682.html
Copyright © 2011-2022 走看看