android RSS example
in this article today learn android RSS Example follow this code.
1.MainActivity.java:
package com.AndroidRss;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import android.app.Activity;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
/**
* Demonstrate using Rome API to parse an RSS feed
* @author Wagied Davies, original version
* @author Ian Darwin, code reorganized to use an AsyncTask instead of
* throwing NetworkOnMainThreadException as the original did.
*/
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText text;
private ListView listView;
private Button goButton;
private Button goDefaultButton;
private Button clearButton;
private ArrayAdapter<String> adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) this.findViewById(R.id.rssURL);
goButton = (Button) this.findViewById(R.id.goButton);
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String rss = text.getText().toString().trim();
if (rss.length() == 0) {
return;
}
try {
Uri.parse(rss);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "Not a valid URL: " + rss, Toast.LENGTH_LONG).show();
return;
}
new RssGetter().execute(rss);
}
});
goDefaultButton = (Button) this.findViewById(R.id.goDefaultButton);
goDefaultButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new RssGetter().execute(getString(R.string.default_feed));
}
});
clearButton = (Button) this.findViewById(R.id.clearButton);
clearButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.clear();
adapter.notifyDataSetChanged();
}
});
listView = (ListView) this.findViewById(R.id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long duration) {
Toast.makeText(MainActivity.this,
"Selected " + adapter.getItem(position) + " @ "
+ position, Toast.LENGTH_SHORT).show();
}
});
adapter = new ArrayAdapter<String>(this, R.layout.dataview,
R.id.ListItemView);
listView.setAdapter(adapter);
}
/**
* The AsyncTask to do the network IO on a background thread
* and the UI updating on, well, the UI thread.
*/
private class RssGetter extends AsyncTask<String, Void, List<SyndEntry>> {
@Override
public List<SyndEntry> doInBackground(String... rss) {
URL feedUrl;
try {
Log.d("DEBUG", "Entered:" + rss);
feedUrl = new URL(rss[0]);
} catch (MalformedURLException e) {
throw new RuntimeException("Invalid URL, try again");
}
SyndFeedInput input = new SyndFeedInput();
try {
SyndFeed feed = input.build(new XmlReader(feedUrl));
@SuppressWarnings("unchecked")
List<SyndEntry> entries = feed.getEntries();
Log.d(TAG, "Retrieved " + entries.size() + " entries");
return entries;
} catch (FeedException | IOException e) {
throw new RuntimeException("Feeding failed: " + e);
}
}
@Override
public void onPostExecute(List<SyndEntry> entries) {
for (SyndEntry ent : entries) {
String title = ent.getTitle();
adapter.add(title);
}
adapter.notifyDataSetChanged();
}
};
}
2.activity_main.xml:
<?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" >
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/rssURL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="RSS URL"
android:inputType="textUri"
android:maxLines="1"
android:singleLine="true" >
</EditText>
<Button
android:id="@+id/goButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/goDefaultButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Project Activity" />
</TableRow>
</TableLayout>
<!-- Mid Panel -->
<ListView
android:id="@+id/ListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<Button
android:id="@+id/clearButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear" >
</Button>
</LinearLayout>
3.dataview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ListItemView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp">
</TextView>
</LinearLayout>
4.strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidRSS</string>
<string name="default_feed">https://www.google.com</string>
</resources>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidRss">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment