Fetching only one item from rss - android

I want to show in my Android app, a single item from rss each day.
Now I dont' want to build a complex rss reader with a listview and so, I just want to get a single item each day, is it possible?
Thanks

This is a very general question, but I'll do my best to respond to it. You'll need to do three things: get the xml, parse the xml, and then display it.
For getting the XML I recommend using an AsyncTaskLoader and an HttpUrlConnection to download it.
For parsing the XML you have a few options. You can use a SAX parser. This is the way I like to do it. You can checkout this tutorial on how to use the SAX parser. Or if you prefer to use a DOM parser, you can checkout this tutorial.
Once it's been parsed, you can just set the contents of a TextView to whatever you parsed.
I don't have an example of all this because this is bascially an entire android app. But with what I've given you, you should have enough to get started and build one on your own.

Fetching the feed with http and parsing the xml can be easily done with a third party library.
Here's code that I used to fetch the Picasa featured photos feed, and parse the rss item to extract the title and the content url.
public void xml_ajax(){
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=8";
aq.ajax(url, XmlDom.class, this, "picasaCb");
}
public void picasaCb(String url, XmlDom xml, AjaxStatus status){
showResult(xml, status);
if(xml == null) return;
List<XmlDom> entries = xml.tags("entry");
List<String> titles = new ArrayList<String>();
String imageUrl = null;
for(XmlDom entry: entries){
titles.add(entry.text("title"));
imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");
}
showTextResult(titles);
}
I also blogged about this here
http://blog.androidquery.com/2011/09/simpler-and-easier-xml-parsing-in.html

Related

Android:Convert Json object to Strings.xml

I have thousand of strings in Json and I wanted to convert into xml(strings.xml) .Is there any short way to do that ? plugins ? or I have to do it one by one?
If you want to convert the json to xml in your app, you can use any existing library. This answer refers to one such.
If you just want to simply convert a json contents to xml, you can use any online converter. This one came first in my google search result. (But I would check the site's reliability before uploading any sensitive data)
You can read the entire JSON into a String object, and get the XML representation with Regex and .replace() method Of a String
Here is a smaple
val json = """{
"text_download_complete":"your file is downloaded"
}"""
val xml = json.replace("\\{\\s*".toRegex(), "<string name=")
.replace(":\\s*\"".toRegex(), ">")
.replace("\"\\s*\\}".toRegex(), "</string>")
Log.d("LOG_TAG", "onCreate: XML:\n$xml")
You can also read the JSON file instead of coping the entire file into a String...
I believe that won't be the optimum solution, but I think it would be a one shot task, and just wanted to be simple rather than looking at performance.

how to put image json data in listview

how to retrieve all images in MySQL to android
really I can retrieve data like (names,ages,....)
but really I can't retrieve all images
whereas I have a table in MySQL contain
(name , age,Image)
thanks for any guide and any help
i'm not sure this will helpful or not but you cant try this. http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
Probably you should consider to store the images in the local storage (instead of in a binary format, which is how it seems you are doing it, from your question), and in your database just store the file location. This way you shouldn't have problems converting the data from and to the database, and the database size wouldn't grow too much.
Then in your list view's adapter, you'll only need to retrieve the image's location and load it into the view you want to display.
EDIT
As I understood, you are sending the data to the Android device via Json. So let's assume you are sending a Json array like this one:
[{"username":"John", "userimage":"http://example.com/images/imagejohn.jpg"},
{"username":"Michael", "userimage":"http://example.com/images/imagemike.jpg"},
{"username":"Sophia", "userimage":"http://example.com/images/imagesophia.jpg"}]
So what you need is to read the JSON contents to an Array and fill it in an adapter. As I mentioned in the comments, you should take a look to the answer here: https://stackoverflow.com/a/8113337/1991053
Basically, you can read the JSON Array and put it into the ListView's Array Adapter, in the example above, you can retrieve each element like this:
JSONArray jsonArray = new JSONArray(input) // input is the JSON string you recieve
for(int i = 0; i < jsonArray.length(); i++) {
String userName = jsonArray.optJSONObject(i).getString("username");
String imageURL = jsonArray.optJSONObject(i).getString("userimage");
// Here you can do whatever you want with the values
// like loading an imageview with the url's data
// or putting the values in an ArrayList for filling up the ListView adapter
}
Of course you can modifiy this to fit in whatever the JSON structure you are using for sending the data, just use JSONObjects and JSONArray for this. Take a look to the documentation here: JSONArray, JSONObject
If you want more control over the JSON data (like serialize or deserialize the data), take a look to Jackson or Gson. There are plenty of examples around to understand how they work.
But I think your best bet is to use the adapter shown Here. Also, for loading up images in a ListView from a URL, you can use this library: Lazy List.
Hope this helps you for a start. Best regards.

How do I grab xml from rss feed and convert that to Json object in Android?

I am new to Android. I want to parse RSS feed. I have this feed "http://www.google.com/ig/api?weather=%27melbourne%27" a weather feed from google. How can I grab the XML data and convert that into Json object that I'll use to populate a listView later on? Or is there any other fast way to parse rss feed in Android? Thank you.
I guess your best bet is to implement a SAXParser to parse the XML content into an Java object (or a list of Java objetcs). I don't think you need to convert it to JSON if you just want to populate the data into a list view.
for more information about a SAXPArser: Google and stackoverflow search are your friends ;)

how to extract web text in android like rss to display it on android GUI?

how to extract some criteria from this page http://www.zigwheels.com/api/zigtvApi.php?method=data&module=News&section=News
and filter this ( content_id , thumbnail, summary , headline , image) to display them as rss feeds in my android GUI
The output from that URL looks like a JSON feed. You can easily parse JSON data in Android using the JsonObject - see this tutorial for a comprehensive example.
A better (and probably easier) solution would be to use Google Gson and extract the object that way. I've written a full (compilable) program you can use as an example here.
You are receiving a JSONArray from that page. You should create some objects from that JSONArray which will be later displayed in a ListView. For retrieving objects from that feed you could use Json or Gson as #Marvin Pinto suggested, or you could also have a look at my ObjectFactory project, which is a very simple and easy to use parser. It can simply create your objects from a json or xml feed, and you could also use it asynchronously.
After you fetch your objects, with your desired option, you can create a ListAdapter and use it to display your objects in your ListView.

How can you parse HTML in android?

I am making an application for android, and an element of the functionality of the application is to return results from an online search of a library's catalogue. The application needs to display the results of the search, which is carried out by way of a custom HTML form, in a manner in keeping with the rest of the application. Ie, the results of the search need to be parsed and the useful elements displayed. I was just wondering if/how this could be achieved in android?
You would use a Html Parser. One that i use and works VERY well is JSoup
This is where you will need to begin with parsing html. Also Apache Jericho is another good one.
You would retrieve the html document by using DOM, and use the JSOUP Select() method to select any tags that you would like to get. Either by tag, id, or class.
Solution
Use the: Jsoup.connect(String url) method:
Document doc = Jsoup.connect("http://example.com/").get();
This will allow you to connect to the html page by using the url. And store it as the Document doc, Through DOM. And the read from it using the selector() method.
Description
The connect(String url) method creates a new Connection, and get()
fetches and parses a HTML file. If an error occurs whilst fetching the
URL, it will throw an IOException, which you should handle
appropriately.
The Connection interface is designed for method chaining to build
specific requests:
Document doc = Jsoup.connect("http://example.com")
If you read through the documentation on Jsoup you should be able to achieve this.
EDIT: Here is how you would use the selector method
//Once the Document is retrieved above, use these selector methods to Extract the data you want by using the tags, id, or css class
Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]");
// img with src ending .png
Element masthead = doc.select("div.masthead").first();
// div with class=masthead
Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
EDIT: Using JSOUP you could use this to get attributes, text,
Document doc = Jsoup.connect("http://example.com")
Element link = doc.select("a").first();
String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""
String linkOuterH = link.outerHtml();
// "<b>example</b>"
String linkInnerH = link.html(); // "<b>example</b>"
You can use XmlPullParser for parsing XML.
For e.g. refer to http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
Being that the search results are HTML and HTML is a Markup Language (ML) you can use Android's XmlPullParser to parse the results.

Categories

Resources