<? 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:id ="@+id/label"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="Enter RSS URL:"
/>
< EditText
android:id ="@+id/rssURL"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:background ="@android:drawable/editbox_background"
android:text ="http://feeds.feedburner.com/MicrosoftUserGroupVinnitsya"
/>
< Button
android:id ="@+id/fetchRss"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginRight ="10dip"
android:text ="Fetch Rss" />
< ListView
android:id ="@+id/rssListView"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" />
</ LinearLayout >
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< TextView
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:padding ="10dp"
android:textSize ="13sp"
android:autoLink ="all" >
</ TextView >
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical"
>
< TextView
android:id ="@+id/titleTextView"
android:text ="Title:"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:padding ="10dp"
android:textSize ="13sp"
android:autoLink ="all"
/>
< TextView
android:id ="@+id/contentTextView"
android:text ="Content:"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textSize ="13sp"
android:autoLink ="all"
/>
</ LinearLayout >
* This source code was highlighted with Source Code Highlighter .
package rembo.network.urss;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class RssItem {
private String title;
private String description;
private Date pubDate;
private String link;
public RssItem( String title, String description, Date pubDate, String link) {
this .title = title;
this .description = description;
this .pubDate = pubDate;
this .link = link;
}
public String getTitle()
{
return this .title;
}
public String getLink()
{
return this .link;
}
public String getDescription()
{
return this .description;
}
public Date getPubDate()
{
return this .pubDate;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd - hh:mm:ss" );
String result = getTitle() + " ( " + sdf.format( this .getPubDate()) + " )" ;
return result;
}
public static ArrayList <RssItem> getRssItems( String feedUrl) {
ArrayList <RssItem> rssItems = new ArrayList <RssItem>();
RssItem rssItemT = new RssItem( "MSUG news" , "Best IT news." ,
new Date(), "http://msug.vn.ua/" );
rssItems.add(rssItemT);
try {
//open an URL connection make GET to the server and
//take xml RSS data
URL url = new URL(feedUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
//DocumentBuilderFactory, DocumentBuilder are used for
//xml parsing
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//using db (Document Builder) parse xml data and assign
//it to Element
Document document = db.parse( is );
Element element = document.getDocumentElement();
//take rss nodes to NodeList
NodeList nodeList = element.getElementsByTagName( "item" );
if (nodeList.getLength() > 0) {
for ( int i = 0; i < nodeList.getLength(); i++) {
//take each entry (corresponds to <item></item> tags in
//xml data
Element entry = (Element) nodeList.item(i);
Element _titleE = (Element) entry.getElementsByTagName(
"title" ).item(0);
Element _descriptionE = (Element) entry
.getElementsByTagName( "description" ).item(0);
Element _pubDateE = (Element) entry
.getElementsByTagName( "pubDate" ).item(0);
Element _linkE = (Element) entry.getElementsByTagName(
"link" ).item(0);
String _title = _titleE.getFirstChild().getNodeValue();
String _description = _descriptionE.getFirstChild().getNodeValue();
Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
String _link = _linkE.getFirstChild().getNodeValue();
//create RssItemObject and add it to the ArrayList
RssItem rssItem = new RssItem(_title, _description,
_pubDate, _link);
rssItems.add(rssItem);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return rssItems;
}
}
* This source code was highlighted with Source Code Highlighter .
package rembo.network.urss;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import android.view.*;
public class RssItemDisplayer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_item_displayer);
RssItem selectedRssItem = RSSactivity.selectedRssItem;
//Bundle extras = getIntent().getExtras();
TextView titleTv = (TextView)findViewById(R.id.titleTextView);
TextView contentTv = (TextView)findViewById(R.id.contentTextView);
String title = "" ;
SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd - hh:mm:ss" );
title = "\n" + selectedRssItem.getTitle() + " ( "
+ sdf.format(selectedRssItem.getPubDate()) + " )\n\n" ;
String content = "" ;
content += selectedRssItem.getDescription() + "\n"
+ selectedRssItem.getLink();
titleTv.setText(title);
contentTv.setText(content);
}
}
* This source code was highlighted with Source Code Highlighter .
package rembo.network.urss;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import android.view.*;
import android.view.View;
import android.widget.*;
import android.content.*;
public class RSSactivity extends Activity {
public static RssItem selectedRssItem = null ;
String feedUrl = "" ;
ListView rssListView = null ;
ArrayList <RssItem> rssItems = new ArrayList <RssItem>();
ArrayAdapter<RssItem> aa = null ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get textview from our layout.xml
final TextView rssURLTV = (TextView) findViewById(R.id.rssURL);
// get button from layout.xml
Button fetchRss = (Button) findViewById(R.id.fetchRss);
// define the action that will be executed when the button is clicked.
fetchRss.setOnClickListener( new View.OnClickListener() {
//@Override
public void onClick(View v) {
feedUrl = rssURLTV.getText().toString();
//TextView TVtitle=(TextView)findViewById(R.id.label);
//CharSequence cs="fetching";
//TVtitle.setText(cs);
aa.notifyDataSetChanged();
refressRssList();
//cs="Feed:";
//TVtitle.setText(cs);
}
});
// get the listview from layout.xml
rssListView = (ListView) findViewById(R.id.rssListView);
// here we specify what to execute when individual list items clicked
rssListView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
//@Override
public void onItemClick(AdapterView<?> av, View view, int index,
long arg3) {
selectedRssItem = rssItems. get (index);
// we call the other activity that shows a single rss item in
// one page
Intent intent = new Intent(
"rembo.network.urss.displayRssItem" );
startActivity(intent);
}
});
//adapters are used to populate list. they take a collection,
//a view (in our example R.layout.list_item
aa = new ArrayAdapter<RssItem>( this , R.layout.list_item, rssItems);
//here we bind array adapter to the list
rssListView.setAdapter(aa);
feedUrl = rssURLTV.getText().toString();
refressRssList();
}
private void refressRssList() {
ArrayList <RssItem> newItems = RssItem.getRssItems(feedUrl);
rssItems.clear();
rssItems.addAll(newItems);
//TextView TVtitle=(TextView)findViewById(R.id.label);
//CharSequence cs="0";
//if(newItems.size()>0) cs="is 1";
//if(newItems.size()>5) cs="is 5";
///TVtitle.setText(cs);
aa.notifyDataSetChanged();
}
}
* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="utf-8" ? >
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="rembo.network.urss"
android:versionCode ="1"
android:versionName ="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".RSSactivity"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name =".RssItemDisplayer" android:label ="Display Rss Item" >
< intent-filter >
< action android:name ="rembo.network.urss.displayRssItem" />
< category android:name ="android.intent.category.DEFAULT" ></ category >
</ intent-filter >
</ activity >
</ application >
< uses-permission android:name ="android.permission.INTERNET" />
</ manifest >
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/98704/
All Articles