Recording the first Android Application Development-it home RSS reader

Source: Internet
Author: User

This semester, the School held an android course, because he has been learning WP development and has been using it. net and Silverlight do not have much time to invest in Android, because I don't want to do Android work after graduation, so I haven't studied much. But at the end of the term, I want to hand in my work. I think it's better to make an RSS reader, because I think the RSS reader is relatively simple when studying WP, the same idea should be used on Android. So last night, with the help of the android gods in our dormitory (mainly to solve some questions raised by WP and Android in different places), I spent more than four hours doing this application.

The general idea is to get the it house's RSS source, save it to a set, and then bind the data to the listview. Click one of the listview items to jump to another page, which has only one webview, this is used to display the webpage corresponding to the URL brought by the click item-of course, the simplest RSS reader in WP is also the idea.

First, define the model of an article:

public class Item {        private String title;    private String description;    private String date;    private String link;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }    public String getDate() {        return date;    }    public void setDate(String date) {        this.date = date;    }    public String getLink() {        return link;    }    public void setLink(String link) {        this.link = link;    }        public HashMap<String,String> getMap(){        HashMap<String, String> map = new HashMap<String, String>();        map.put("title", title);        map.put("description", description);        map.put("date", date);        map.put("link", link);        return map;    }}

The last method getmap is used to return the attributes of the current object in the form of hashmap. Why? Because it will be used later.

 

How to obtain an RSS feed:

Public static list <Map <string, string> readxml (inputstream instream) {list <Map <string, string> DATA = new arraylist <Map <string, string >>(); documentbuilderfactory factory = documentbuilderfactory. newinstance (); try {documentbuilder builder = factory. newdocumentbuilder (); document dom = builder. parse (instream); element root = Dom. getdocumentelement (); nodelist items = root. getelementsbytagname ("item"); // query all item nodes for (INT I = 0; I <items. getlength (); I ++) {item = new item (); // obtain the first item node element itemnode = (element) items. item (I); // obtain all subnodes under the item node nodelist childsnodes = itemnode. getchildnodes (); For (Int J = 0; j <childsnodes. getlength (); j ++) {node = (node) childsnodes. item (j); // determines whether the element type is if (node. getnodetype () = node. element_node) {element childnode = (element) node; If ("title ". equals (childnode. getnodename () {item. settitle (childnode. getfirstchild (). getnodevalue ();} else if ("Link ". equals (childnode. getnodename () {string computer = childnode. getfirstchild (). getnodevalue (); string phone = "http://wap.ithome.com/html" + computer. substring (computer. lastindexof ("/"); log. E ("jpho", phone); item. setlink (phone);} else if ("Description ". equals (childnode. getnodename () {item. setdescription (childnode. getfirstchild (). getnodevalue ();} else if ("pubdate ". equals (childnode. getnodename () {item. setdate (childnode. getfirstchild (). getnodevalue () ;}} data. add (item. getmap ();} instream. close ();} catch (exception e) {e. printstacktrace ();} return data ;}

This method adds the obtained RSS data to a list set and uses the getmap method mentioned above. Here we made a small deal, that is, converting the obtained article link into a mobile phone version link. After all, we need to see on our mobile phones that loading is fast and traffic is saved.

 

The logic is as follows:

String[] from = new String[] { "title", "date", "description", "link" };        int[] to = new int[] { R.id.title, R.id.date, R.id.description,                R.id.link };        URL url;        List<Map<String, String>> data = new ArrayList<Map<String, String>>();        try {            url = new URL("http://www.ithome.com/rss/");            HttpURLConnection conn = (HttpURLConnection) url.openConnection();            InputStream isr = conn.getInputStream();            data = readXML(isr);        } catch (MalformedURLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,                from, to);        this.getListView().setAdapter(adapter);        this.getListView().setOnItemClickListener(this);

Data Binding here is a little different from WP writing. Here, we use an adapter to bind the property to the corresponding control. In WP, we can directly write bingding. Getlistview (). setadapter (adatper) is similar to setting the data context in WP, but here we set the data adapter.

Next is the click event of item:

@Override    public void onItemClick(AdapterView<?> parent, View view, int position,            long id) {        // TODO Auto-generated method stub        ListView listView = (ListView) parent;        HashMap<String, String> map = (HashMap<String, String>) listView                .getItemAtPosition(position);        String url = map.get("link");        Intent intent = new Intent(getApplicationContext(), ItemActivity.class);        intent.putExtra("url", url);        startActivity(intent);    }

Intent is a bit like the application settings in WP. It also saves data to an independent bucket through key-value pairs and obtains it on other pages. The logic of the article page is as follows:

@SuppressLint("SetJavaScriptEnabled")public class ItemActivity extends Activity {    private WebView mWebView;    @Override    protected void onCreate(Bundle icicle) {        super.onCreate(icicle);        setContentView(R.layout.activity_item);        Intent intent = getIntent();        mWebView = (WebView) findViewById(R.id.webView);        mWebView.getSettings().setJavaScriptEnabled(true);        mWebView.loadUrl(intent.getStringExtra("url"));    }}

To enable webview to load JS, add this sentence: @ suppresslint ("setjavascriptenabled") and mwebview. getsettings (). setjavascriptenabled (true), then obtain the passed link in the oncreate method, and display it with webview.

This is the general idea. After all, this is the first time you write an Android app. If you do not write well, you can still correct it. However, the idea is the same as that of WP, so as long as you master a mobile development method, turning to other platforms is just a language problem and there are some different features, the main idea is the same. However, I do not plan to engage in Android development in the future. This may be the first and the last Android application development. This article is only used to commemorate things that have not yet ended.

If necessary, you can download the project code here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.