|
1 |
| -package com.pfizer.android; |
2 |
| - |
3 |
| -import java.io.IOException; |
4 |
| -import java.net.MalformedURLException; |
5 |
| -import java.net.URL; |
6 |
| -import java.util.List; |
7 |
| - |
8 |
| -import com.sun.syndication.feed.synd.SyndEntry; |
9 |
| -import com.sun.syndication.feed.synd.SyndFeed; |
10 |
| -import com.sun.syndication.io.FeedException; |
11 |
| -import com.sun.syndication.io.SyndFeedInput; |
12 |
| -import com.sun.syndication.io.XmlReader; |
13 |
| - |
14 |
| -import android.app.Activity; |
15 |
| -import android.os.AsyncTask; |
16 |
| -import android.os.Bundle; |
17 |
| -import android.util.Log; |
18 |
| -import android.view.View; |
19 |
| -import android.view.View.OnClickListener; |
20 |
| -import android.widget.AdapterView; |
21 |
| -import android.widget.AdapterView.OnItemClickListener; |
22 |
| -import android.widget.ArrayAdapter; |
23 |
| -import android.widget.Button; |
24 |
| -import android.widget.EditText; |
25 |
| -import android.widget.ListView; |
26 |
| -import android.widget.Toast; |
27 |
| - |
28 |
| -/** |
29 |
| - * Demonstrate using Rome API to parse an RSS feed |
30 |
| - * @author Wagied Davies, original version |
31 |
| - * @author Ian Darwin, code reorganized to use an AsyncTask instead of |
32 |
| - * throwing NetworkOnMainThreadException as the original did. |
33 |
| - */ |
34 |
| -public class AndroidRss extends Activity { |
35 |
| - private static final String TAG = AndroidRss.class.getSimpleName(); |
36 |
| - private EditText text; |
37 |
| - private ListView listView; |
38 |
| - private Button goButton; |
39 |
| - private Button goDefaultButton; |
40 |
| - private Button clearButton; |
41 |
| - private ArrayAdapter<String> adapter = null; |
42 |
| - |
43 |
| - @Override |
44 |
| - public void onCreate(Bundle savedInstanceState) { |
45 |
| - super.onCreate(savedInstanceState); |
46 |
| - setContentView(R.layout.main); |
47 |
| - |
48 |
| - text = (EditText) this.findViewById(R.id.rssURL); |
49 |
| - goButton = (Button) this.findViewById(R.id.goButton); |
50 |
| - goButton.setOnClickListener(new OnClickListener() { |
51 |
| - @Override |
52 |
| - public void onClick(View v) { |
53 |
| - new RssGetter().execute(text.getText().toString().trim()); |
54 |
| - } |
55 |
| - }); |
56 |
| - goDefaultButton = (Button) this.findViewById(R.id.goDefaultButton); |
57 |
| - goDefaultButton.setOnClickListener(new OnClickListener() { |
58 |
| - @Override |
59 |
| - public void onClick(View v) { |
60 |
| - new RssGetter().execute(getString(R.string.default_feed)); |
61 |
| - } |
62 |
| - }); |
63 |
| - |
64 |
| - clearButton = (Button) this.findViewById(R.id.clearButton); |
65 |
| - clearButton.setOnClickListener(new OnClickListener() { |
66 |
| - @Override |
67 |
| - public void onClick(View v) { |
68 |
| - adapter.clear(); |
69 |
| - adapter.notifyDataSetChanged(); |
70 |
| - } |
71 |
| - }); |
72 |
| - |
73 |
| - listView = (ListView) this.findViewById(R.id.ListView); |
74 |
| - listView.setOnItemClickListener(new OnItemClickListener() { |
75 |
| - @Override |
76 |
| - public void onItemClick(AdapterView<?> parent, View view, |
77 |
| - int position, long duration) { |
78 |
| - Toast.makeText(AndroidRss.this, |
79 |
| - "Selected " + adapter.getItem(position) + " @ " |
80 |
| - + position, Toast.LENGTH_SHORT).show(); |
81 |
| - } |
82 |
| - }); |
83 |
| - |
84 |
| - adapter = new ArrayAdapter<String>(this, R.layout.dataview, |
85 |
| - R.id.ListItemView); |
86 |
| - listView.setAdapter(adapter); |
87 |
| - } |
88 |
| - |
89 |
| - /** |
90 |
| - * The AsyncTask to do the network IO on a background thread |
91 |
| - * and the UI updating on, well, the UI thread. |
92 |
| - */ |
93 |
| - private class RssGetter extends AsyncTask<String, Void, List<SyndEntry>> { |
94 |
| - |
95 |
| - @Override |
96 |
| - public List<SyndEntry> doInBackground(String... rss) { |
97 |
| - |
98 |
| - URL feedUrl; |
99 |
| - try { |
100 |
| - Log.d("DEBUG", "Entered:" + rss); |
101 |
| - feedUrl = new URL(rss[0]); |
102 |
| - } catch (MalformedURLException e) { |
103 |
| - throw new RuntimeException("Invalid URL, try again"); |
104 |
| - } |
105 |
| - |
106 |
| - SyndFeedInput input = new SyndFeedInput(); |
107 |
| - try { |
108 |
| - SyndFeed feed = input.build(new XmlReader(feedUrl)); |
109 |
| - @SuppressWarnings("unchecked") |
110 |
| - List<SyndEntry> entries = feed.getEntries(); |
111 |
| - Log.d(TAG, "Retrieved " + entries.size() + " entries"); |
112 |
| - return entries; |
113 |
| - } catch (FeedException | IOException e) { |
114 |
| - throw new RuntimeException("Feeding failed: " + e); |
115 |
| - } |
116 |
| - } |
117 |
| - |
118 |
| - @Override |
119 |
| - public void onPostExecute(List<SyndEntry> entries) { |
120 |
| - for (SyndEntry ent : entries) { |
121 |
| - String title = ent.getTitle(); |
122 |
| - adapter.add(title); |
123 |
| - } |
124 |
| - adapter.notifyDataSetChanged(); |
125 |
| - } |
126 |
| - }; |
127 |
| -} |
| 1 | +package com.pfizer.android; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import com.sun.syndication.feed.synd.SyndEntry; |
| 9 | +import com.sun.syndication.feed.synd.SyndFeed; |
| 10 | +import com.sun.syndication.io.FeedException; |
| 11 | +import com.sun.syndication.io.SyndFeedInput; |
| 12 | +import com.sun.syndication.io.XmlReader; |
| 13 | + |
| 14 | +import android.app.Activity; |
| 15 | +import android.net.Uri; |
| 16 | +import android.os.AsyncTask; |
| 17 | +import android.os.Bundle; |
| 18 | +import android.util.Log; |
| 19 | +import android.view.View; |
| 20 | +import android.view.View.OnClickListener; |
| 21 | +import android.widget.AdapterView; |
| 22 | +import android.widget.AdapterView.OnItemClickListener; |
| 23 | +import android.widget.ArrayAdapter; |
| 24 | +import android.widget.Button; |
| 25 | +import android.widget.EditText; |
| 26 | +import android.widget.ListView; |
| 27 | +import android.widget.Toast; |
| 28 | + |
| 29 | +/** |
| 30 | + * Demonstrate using Rome API to parse an RSS feed |
| 31 | + * @author Wagied Davies, original version |
| 32 | + * @author Ian Darwin, code reorganized to use an AsyncTask instead of |
| 33 | + * throwing NetworkOnMainThreadException as the original did. |
| 34 | + */ |
| 35 | +public class AndroidRss extends Activity { |
| 36 | + private static final String TAG = AndroidRss.class.getSimpleName(); |
| 37 | + private EditText text; |
| 38 | + private ListView listView; |
| 39 | + private Button goButton; |
| 40 | + private Button goDefaultButton; |
| 41 | + private Button clearButton; |
| 42 | + private ArrayAdapter<String> adapter = null; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onCreate(Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + setContentView(R.layout.main); |
| 48 | + |
| 49 | + text = (EditText) this.findViewById(R.id.rssURL); |
| 50 | + goButton = (Button) this.findViewById(R.id.goButton); |
| 51 | + goButton.setOnClickListener(new OnClickListener() { |
| 52 | + @Override |
| 53 | + public void onClick(View v) { |
| 54 | + String rss = text.getText().toString().trim(); |
| 55 | + if (rss.length() == 0) { |
| 56 | + return; |
| 57 | + } |
| 58 | + try { |
| 59 | + Uri.parse(rss); |
| 60 | + } catch (Exception e) { |
| 61 | + Toast.makeText(AndroidRss.this, "Not a valid URL: " + rss, Toast.LENGTH_LONG).show(); |
| 62 | + return; |
| 63 | + } |
| 64 | + new RssGetter().execute(rss); |
| 65 | + } |
| 66 | + }); |
| 67 | + goDefaultButton = (Button) this.findViewById(R.id.goDefaultButton); |
| 68 | + goDefaultButton.setOnClickListener(new OnClickListener() { |
| 69 | + @Override |
| 70 | + public void onClick(View v) { |
| 71 | + new RssGetter().execute(getString(R.string.default_feed)); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + clearButton = (Button) this.findViewById(R.id.clearButton); |
| 76 | + clearButton.setOnClickListener(new OnClickListener() { |
| 77 | + @Override |
| 78 | + public void onClick(View v) { |
| 79 | + adapter.clear(); |
| 80 | + adapter.notifyDataSetChanged(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + listView = (ListView) this.findViewById(R.id.ListView); |
| 85 | + listView.setOnItemClickListener(new OnItemClickListener() { |
| 86 | + @Override |
| 87 | + public void onItemClick(AdapterView<?> parent, View view, |
| 88 | + int position, long duration) { |
| 89 | + Toast.makeText(AndroidRss.this, |
| 90 | + "Selected " + adapter.getItem(position) + " @ " |
| 91 | + + position, Toast.LENGTH_SHORT).show(); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + adapter = new ArrayAdapter<String>(this, R.layout.dataview, |
| 96 | + R.id.ListItemView); |
| 97 | + listView.setAdapter(adapter); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The AsyncTask to do the network IO on a background thread |
| 102 | + * and the UI updating on, well, the UI thread. |
| 103 | + */ |
| 104 | + private class RssGetter extends AsyncTask<String, Void, List<SyndEntry>> { |
| 105 | + |
| 106 | + @Override |
| 107 | + public List<SyndEntry> doInBackground(String... rss) { |
| 108 | + |
| 109 | + URL feedUrl; |
| 110 | + try { |
| 111 | + Log.d("DEBUG", "Entered:" + rss); |
| 112 | + feedUrl = new URL(rss[0]); |
| 113 | + } catch (MalformedURLException e) { |
| 114 | + throw new RuntimeException("Invalid URL, try again"); |
| 115 | + } |
| 116 | + |
| 117 | + SyndFeedInput input = new SyndFeedInput(); |
| 118 | + try { |
| 119 | + SyndFeed feed = input.build(new XmlReader(feedUrl)); |
| 120 | + @SuppressWarnings("unchecked") |
| 121 | + List<SyndEntry> entries = feed.getEntries(); |
| 122 | + Log.d(TAG, "Retrieved " + entries.size() + " entries"); |
| 123 | + return entries; |
| 124 | + } catch (FeedException | IOException e) { |
| 125 | + throw new RuntimeException("Feeding failed: " + e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onPostExecute(List<SyndEntry> entries) { |
| 131 | + for (SyndEntry ent : entries) { |
| 132 | + String title = ent.getTitle(); |
| 133 | + adapter.add(title); |
| 134 | + } |
| 135 | + adapter.notifyDataSetChanged(); |
| 136 | + } |
| 137 | + }; |
| 138 | +} |
0 commit comments