Faruk Yazici
Faruk Yazici

Reputation: 2404

"no value for rss", though it's obviously there

I am trying to read RSS feeds from CNN in an Android project. So far everything is going right. I successfully made the connection and retrieved the whole XML file as one string. Then I tried to create a JSON Object and parse it. However some part of it couldn't be read. The XML I tried to read is this

view-source:http://rss.cnn.com/rss/edition_world.rss

In order to simplify and make everything more clear, I attached a picture of the RSS feed viewed in a JSON Object Editor:

http://tinypic.com/view.php?pic=2iiaezb&s=8#.U7ax0vmGFmw

So the code is like this,

//It successfully converts the text to JSON
JSONObject jObj = new JSONObject(responseText);

String respTime = jObj.getString("responseTime");
//It successfully prints the responseTime
System.out.println("Response time is: " + respTime);

JSONObject respHeader = jObj.getJSONObject("responseHeaders");
String date = respHeader.getString("date");
//It successfully prints the date as well
System.out.println("Date is: "+ date);


//However it says no value for rss found
JSONObject rssObj  = jObj.getJSONObject("rss");
JSONObject channelObj = rssObj.getJSONObject("channel");
JSONArray itemArr = channelObj.getJSONArray("item");

"responseTime", "responseHeaders" and "rss" are all equivalent in terms of hierarchical structure of the XML file, as you can see from the image I referenced. So while I am able to read "responseTime" and "responseHeaders", why does it say that no value found for "rss", and therefore I am unable to reach any of it's sub-items ?

Upvotes: 1

Views: 41

Answers (1)

Faruk Yazici
Faruk Yazici

Reputation: 2404

I found out the answer. The actual JSON object string, namely the responseText, is not identical to what appears in the XML file. It is something different and it took time for me to realize it. I couldn't copy it from the Eclipse's logcat since it has a limited buffer, so I had to study it by writing to a text file. Then I could parse it correctly.

Upvotes: 1

Related Questions