Sabtu, 30 Juni 2012

Membuat RSS Reader Android (Pemrograman Android)

Kita sudah tidak asing dengan RSS (Really Simple Syndication). RSS adalah rangkuman (ringkasan) dari suatu web yang boleh dipublikasikan atau di link-kan oleh web lain. Misal Detik.com mempunyai RSS berisi berita-berita terbaru, Kompas.com dan sebagainya.

Kali ini kita buat RSS Reader atau Pembaca RSS menggunakan Aplikasi Android.

Pertama-tama program utama kita letakkan di Main Activity

MainActivity.java

package my.amijaya.rssreadersederhana;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
String streamTitle = "";
String daftarjudul = "";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView result = (TextView)findViewById(R.id.result);
        
        try {
URL rssUrl = new URL("http://rss.detik.com/index.php/detikcom"); 
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);

//result.setText(streamTitle);
result.setText(daftarjudul);

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result.setText("Cannot connect RSS!");
}
        
        
    }
    
    private class RSSHandler extends DefaultHandler
    {
    final int stateUnknown = 0;
    final int stateTitle = 1;
    int state = stateUnknown;
   
    int numberOfTitle = 0;
    String strTitle = "";
    String strElement = "";
   
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
strTitle = "--- Start Document ---\n";
}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
strTitle += "--- End Document ---";
streamTitle = "Number Of Title: " + String.valueOf(numberOfTitle) + "\n"
+ strTitle;
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("title"))
{
state = stateTitle;
strElement = "Title: ";
numberOfTitle++;
}
else
{
state = stateUnknown;
}
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("title"))
{
strTitle += strElement + "\n";
daftarjudul += strElement + "\n";
}
state = stateUnknown;
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String strCharacters = new String(ch, start, length);
if (state == stateTitle)
{
strElement += strCharacters;
}
}
   
    }
}

Kemudian pada desain tampilannya kita buat tampilan sederhana berisi satu TextView untuk menampilkan daftar judul-judul dari RSS yang dibaca :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" />
<ScrollView 
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/result" />
</ScrollView>
</LinearLayout>


Kemudian jangan lupa pada Android Manifest, berikan permission untuk terhubung (connect) ke Internet :

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="my.amijaya.rssreadersederhana"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 


Source Code selengkapnya berbentuk project dapat didownload pada link di bawah ini :

https://docs.google.com/open?id=0B4i1FYc_4RXzRGI0VjVKT1VkSWs

Semoga berguna.