Android RSS Reader 2.0
Well, its now 2013, and I have updated a version of this post - a long with a complete working application on GitHub - you can find that here it covers Android 3.0+ so includes fragments and parsing the RSS in an AsyncTask. Of course, you can still read this post and access the code for the pre-3.0 version!Since posting a link to my simple RSS Reader for Android implemented using a custom SAX parser I have some questions regarding upgrading to handle images - I have previously posted on how to implement new tags in the feed that you want to handle (including nested tags) - but one question I received was regarding when images are just embedded in the text (e.g. not a specific image tag, but say as part of a description as HTML).
So, I have updated the code (uploaded, see the sidebar) - this time it parses the description text data to retrieve any Image links its pretty grim string manipulation, but it does the job!
As you can see, it now parses the image URL from the description and inserts it to the left of the feed item.
First I needed to change the Article class to parse the link. I updated the setDescription method to also check for IMAGE tags to parse, if any found then it would set the ImgLink member:
public void setDescription(String description) { this.description = description; //parse description for any image or video links if (description.contains("<img ")){ String img = description.substring(description.indexOf("<img ")); String cleanUp = img.substring(0, img.indexOf(">")+1); img = img.substring(img.indexOf("src=") + 5); int indexOf = img.indexOf("'"); if (indexOf==-1){ indexOf = img.indexOf("\""); } img = img.substring(0, indexOf); setImgLink(img); this.description = this.description.replace(cleanUp, ""); } }
I then also had to update the RssListAdapter class - this needed the necessary action to pull down the link based on our parsed URL and then inflate the ImageView in our row:
if (jsonImageText.get("imageLink") != null){ String url = (String) jsonImageText.get("imageLink"); URL feedImage= new URL(url); HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection(); InputStream is = conn.getInputStream(); Bitmap img = BitmapFactory.decodeStream(is); imageView.setImageBitmap(img); }
These blog articles are very helpful. How would one go about making the rss articles so that when they're clicked, they will open to a link or webview?
ReplyDeleteSame question about Mike here. I tried to get position from the Json object, get the 'url' string, then pass it to intent, but I still didn't get it to work. Can you help? Thanks a lot
ReplyDeletehi. can you help me? i have some problems reading rss coincidentally all 2.0. the rest works very well.
ReplyDeletei have problem with rss http://rss.tecnologia.uol.com.br/ultnot/index.xml
and
http://olhardigital.uol.com.br/rss/ultimas_noticias.php
thanks
Im guessing this will only return the first image of a post....
ReplyDeleteMike and Sony
ReplyDeleteIn RSSReader class add in buildJsonObject funciton:
String urlLink = article.getUrl().toString();
current.put("URLLink", urlLink);
In RSSActivity Class add a listner for item click.
In the end off onCreate function add:
current.put("URLLink", urlLink);
Also add the listner function inside onCreate function:
OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position, long rowid) {
JSONObject jsonURL = (JSONObject) parent.getItemAtPosition(position);
//System.out.println("XXXX Link found!");
String url = null;
try {
url = (String) jsonURL.get("URLLink");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URL webURL = null;
try {
webURL = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL.toString()));
startActivity(myIntent);
}
};
Can you please add the whole code to have a listener in ListView items ? What do you mean by" In the end off onCreate function add:
Deletecurrent.put("URLLink", urlLink);" in RSSActivity class?
please, that would be very nice!
DeleteThank you so much for this example!
ReplyDeletethank you so much for this brief explanation... but i was wondering, why the scroll function is kinda laggy? even in my samsung galaxy s2? and sometimes it's stop working and i must force close it...
ReplyDeleteany suggestions?
Hei, thanks for such a great tute. however, i'm getting an error message when changing the URL feed.
ReplyDeletei get an error :
01-13 17:50:35.919: ERROR/RSS Handler IO(281): Too many redirects >> java.net.ProtocolException: Too many redirects
Here is my feed URL :
http://www.flightstats.com/go/rss/flightStatusByRoute.do?guid=34b64945a69b9cac:-420d81e6:134c55649d7:-21c5&departureCode=cgk&arrivalCode=sub&departureDate=2012-01-13
thank you. :D
Please when i open the app to view RSS feeds i have JSON Exception on every element on the ListView. Any idea please?
ReplyDeleteI get the same error: JSON Exception!! Please help!!
ReplyDeletehi this code not working with url rss/format=atom10. can you help me
ReplyDeleteThank you very much
ReplyDeleteI was looking for tutorial looks like this for two months
but I have same problem when I want to change the feed url
JSON Exception
any help ?
Thanks so much
ReplyDeleteI face a problem when I try to get the description contain multip-lines. The result is only the first line. Could you help to advise this issue.