2012-04-03 67 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); 
    } 
}