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("//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(//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();*/
}
}