java使用TimerTask定时器获取指定网络数据
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class GetYinInfo extends TimerTask {
private void getCOMEXInfo() throws IOException{
String res = "";
SimpleDateFormat dateformat=new SimpleDateFormat("HH:mm:ss");
String df=dateformat.format(new Date());
URL url = new URL("https://www.jb51.netI");
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader bf = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "GBK"));
String line;
while ((line = bf.readLine()) != null) {
res += line;
}
String AGTD[]=res.split(",");
String re[]=AGTD[0].split(""");
System.out.println("COMEX "+df+":"+re[1]);
bf.close();
}
private void getTDInfo() throws IOException{
String res = "";
SimpleDateFormat dateformat=new SimpleDateFormat("HH:mm:ss");
String df=dateformat.format(new Date());
URL url = new URL(https://www.jb51.net);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader bf = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "GBK"));
String line;
while ((line = bf.readLine()) != null) {
res += line;
}
String AGTD[]=res.split(",");
String re[]=AGTD[0].split(""");
System.out.println("AG "+df+":"+re[1]);
bf.close();
}
@Override
public void run() {
try {
getCOMEXInfo();
getTDInfo();
System.out.println("---------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask t1 = new GetYinInfo();
//1000毫秒后,每隔1000毫秒运行一次t1任务
timer.schedule(t1,1000,5000);
/*try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();*/
}
}
相关文章
Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题
这篇文章主要介绍了Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下2016-10-10java使用短信设备发送sms短信的示例(java发送短信)
这篇文章主要介绍了java使用短信设备发送sms短信的示例(java发送短信),需要的朋友可以参考下2014-04-04springboot基于keytool实现https的双向认证示例教程
这篇文章主要介绍了springboot基于keytool实现https的双向认证,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-06-06java多线程之线程,进程和Synchronized概念初解
这篇文章主要介绍了java多线程之线程,进程和Synchronized概念初解,涉及进程与线程的简单概念,实现多线程的方式,线程安全问题,synchronized修饰符等相关内容,具有一定借鉴价值,需要的朋友可以参考下。2017-11-11Spring扩展点之BeanFactoryPostProcessor详解
这篇文章主要介绍了Spring扩展点之BeanFactoryPostProcessor详解,Spring的设计非常优雅,有很多的扩展点供我们对项目进行扩展,今天学习一下Spring其中扩展点之一的BeanFactoryPostProcessor,需要的朋友可以参考下2023-11-11HttpServletRequest对象常用功能_动力节点Java学院整理
这篇文章主要为大家详细介绍了HttpServletRequest对象常用功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-07-07
最新评论