我需要从RSS源每5分钟更新一次新闻。
我已经编写了一个TimerTask,如下所示:
public class TimerTaskForAllNews
{
public static void main( String[] args )
{
TimerTask task = new AllNewsUpdatrUtility();
Timer timer = new Timer();
timer.schedule(task, 1000,60000);
}
}
这是我的计时器任务实现类。
package com.util;
import java.net.URL;
public class AllNewsUpdatrUtility extends TimerTask {
private static AllNewsUpdatrUtility instance = null;
public AllNewsUpdatrUtility() {}
public static AllNewsUpdatrUtility getInstance() {
if (instance == null)
instance = new AllNewsUpdatrUtility();
return instance;
}
@Override
public void run() {
try {
JSONArray latestnews = new JSONArray();
JSONObject jsonobj_allnews = new JSONObject();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL url = new URL("http://www.rssmix.com/u/8160628/rss.xml");
Document doc = builder.parse(url.openStream());
NodeList items = doc.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
String title = getValue(item, "title");
String link = getValue(item, "link");
String pub_date = getValue(item, "pubDate");
} // for loop ends here
} catch (Exception e) {
e.printStackTrace();
}
}
}
请问我如何改进这个程序呢?