java使用TimerTask定時(shí)器獲取指定網(wǎng)絡(luò)數(shù)據(jù)
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("http://www.dhdzp.comI");
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(http://www.dhdzp.com);
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毫秒運(yùn)行一次t1任務(wù)
timer.schedule(t1,1000,5000);
/*try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
timer.cancel();*/
}
}
相關(guān)文章
SpringBoot中LogBack日志配置與多環(huán)境實(shí)戰(zhàn)
在現(xiàn)代軟件開(kāi)發(fā)中,日志記錄是不可或缺的一部分,Spring Boot 提供了多種日志框架的支持,其中 Logback 是一個(gè)非常流行的選擇,因?yàn)樗?jiǎn)單、高效且功能強(qiáng)大,本文將介紹如何在 Spring Boot 項(xiàng)目中配置 Logback,并實(shí)現(xiàn)不同環(huán)境下的日志配置,需要的朋友可以參考下2025-01-01
java多次嵌套循環(huán)查詢(xún)數(shù)據(jù)庫(kù)導(dǎo)致代碼中數(shù)據(jù)處理慢的解決
這篇文章主要介紹了java多次嵌套循環(huán)查詢(xún)數(shù)據(jù)庫(kù)導(dǎo)致代碼中數(shù)據(jù)處理慢的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java的String類(lèi)中的startsWith方法和endsWith方法示例詳解
大家應(yīng)該都知道startsWith()方法用于檢測(cè)字符串是否以指定的前綴開(kāi)始,endsWith()方法用于測(cè)試字符串是否以指定的后綴結(jié)束,本文就Java的String類(lèi)中的startsWith方法和endsWith方法給大家詳細(xì)講解,感興趣的朋友一起看看吧2023-11-11
java基礎(chǔ)詳解之?dāng)?shù)據(jù)類(lèi)型知識(shí)點(diǎn)總結(jié)
這篇文章主要介紹了java基礎(chǔ)詳解之?dāng)?shù)據(jù)類(lèi)型知識(shí)點(diǎn)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04

