2012-04-03 69 views
0

所以我有一個偉大的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);它不顯示任何錯誤,但是當我啓動我的應用程序,它加載新聞時崩潰。

有人可以幫助我嗎?對不起,英文不好。

+0

您可以張貼控制檯日誌 – 2012-04-03 16:31:31

+0

對不起,我上的Java/Android的新。你問我logcat消息? – 2012-04-03 17:42:12

回答

0

解析此:

arrays.PodcastPubDate[i] = pubdate.item(0).getChildNodes().item(0).getNodeValue(); 

取而代之的NodeList?我假設這是String;你不能僅僅解析一個NodeList,它沒有任何邏輯意義。

0

您不僅有XML解析問題。你也有TIMEFORMAT解析的問題太多

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z"); 
Date formatedDate = sdf.parse(((Node)((Element)pubdate.item(0)).getChildNodes().item(0)).getNodeValue()); 
+0

thanx,但應用程序崩潰無論如何 http://dl.dropbox.com/u/6151466/Gaudeamus/logcat.txt 我不能添加錯誤消息列表,因爲太多的文本,所以我添加日誌到文本文件 – 2012-04-03 17:57:44

0

嘗試指定美國格式:

public void setPubDate(String pubDate) { 
    try { 
     SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US); 
     this.pubDate = dateFormat.parse(pubDate); 

    } catch (java.text.ParseException e) { 
     Log.e("MySoft", title); 
    } 
}