所以我有一個偉大的RSS閱讀器源代碼,它的作品完美,但我有一個問題,我需要將日期從2012年3月30日星期五05:09:20 +0000轉換爲簡單格式「yyyy/MM/dd hh:mm:ss」,但是我不能使它工作,因爲衝突了兩種數據類型,NodeList和Date。RSS閱讀器錯誤,同時格式化日期
public class rssparser {
private static NodeList newdate;
private static NodeList formmatter;
private static NodeList formatter;
private static Intent event;
private static ResourceBundle bundle;
private static NodeList pubdate1;
public static void parse(){
URL url;
try {
url = new URL("http://www.gaudeamus.fm/feed/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
doc = db.parse(url.openStream());
doc.getDocumentElement().normalize();
NodeList itemLst = doc.getElementsByTagName("item");
arrays.PodcastTitle = new String[itemLst.getLength()];
arrays.PodcastURL = new String[itemLst.getLength()];
arrays.PodcastContent = new String[itemLst.getLength()];
arrays.PodcastMedia = new String[itemLst.getLength()];
arrays.PodcastPubDate = new String[itemLst.getLength()];
// SimpleDateFormat pubdate = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
// Date formmater = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");
for(int i=0; i < itemLst.getLength(); i++){
Node item = itemLst.item(i);
if(item.getNodeType() == Node.ELEMENT_NODE) {
Element ielem = (Element) item;
NodeList title = ielem.getElementsByTagName("title");
NodeList link = ielem.getElementsByTagName("link");
NodeList pubdate = ielem.getElementsByTagName("pubDate");
//NodeList description = ielem.getElementsByTagName("description");
NodeList content = ielem.getElementsByTagName("content:encoded");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String formatedDate =sdf.parse(pubdate);
arrays.PodcastTitle[i] = title.item(0).getChildNodes().item(0).getNodeValue();
arrays.PodcastTitle[i] +=formatedDate+" \n" + pubdate.item(0).getChildNodes().item(0).getNodeValue();
arrays.PodcastURL[i] = link.item(0).getChildNodes().item(0).getNodeValue();
arrays.PodcastContent[i] = content.item(0).getChildNodes().item(0).getNodeValue();
arrays.PodcastPubDate[i] = pubdate.item(0).getChildNodes().item(0).getNodeValue();
//arrays.PodcastMedia[i] = mediaurl;
}
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這是一個代碼,
String formatedDate =sdf.parse(pubdate);
但我的問題是,需要解析字符串類型,但我有節點列表,我嘗試了很多變種,但它不會工作。如果我不解析,但格式如String formatedDate =sdf.format(pubdate);
它不顯示任何錯誤,但是當我啓動我的應用程序,它加載新聞時崩潰。
有人可以幫助我嗎?對不起,英文不好。
您可以張貼控制檯日誌 – 2012-04-03 16:31:31
對不起,我上的Java/Android的新。你問我logcat消息? – 2012-04-03 17:42:12