我一直在研究RSS阅读器应用程序。到目前为止,我已经学会了从Web解析xml数据并显示所需的数据。我使用了Busy Coder的指南和Derek Banas的教程来研究这个主题,并建立了一个Stock Quote读者。
无论如何,我想创建一个RSS阅读器来阅读来自某个新闻网站的提要。问题是(据我所知),当我点击this page的RSS链接时,网页会呈现xml本身(如果我错了,请纠正我)。所以我右键单击并使用“查看源代码”从源代码中获取xml
现在编辑了一些代码并尝试使用教程中的链接确认我的程序运行正常后,我使用了新的查看源URL,但应用程序停止了工作。
通过doInBackground中的try-catch块,我得到了异常 - “java.net.MalformedURLException:Unknown protocol:view = source”。
那么,有没有办法读取处理此异常并使解析器读取xml文件? 谢谢,这是我的CricketActivity代码。在MainActivity中按下按钮时调用它。
package com.example.try_rssfeed;
public class CricketActivity extends ListActivity {
private static final String TAG = "CRICKET"; // For Logcat
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()));
final String cricketURL = "view-source:http://timesofindia.feedsportal.com/c/33039/f/533920/index.rss";
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new MyAsyncTask().execute(cricketURL);
} else {
Log.d(TAG, "No network connection available.");
}
}
private class MyAsyncTask extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... args) {
Log.d(TAG, "In doBackground ");
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
InputStream streamUrl = loadXmlFromNetwork(args[0]);
Log.d(TAG, "stream " + streamUrl.toString());
xpp.setInput(streamUrl, null);
Log.d(TAG, "After setInput " + xpp.getEventType());
int count = 1; // For Logcat
do {
Log.d(TAG, "in While " + count);
if (xpp.getEventType() == XmlPullParser.START_TAG && xpp.getName().equals("title")) {
xpp.next();
Log.d(TAG, xpp.getEventType() + "____" + count++);
publishProgress(xpp.getText());
}
xpp.next();
} while (xpp.getEventType() != XmlPullParser.END_DOCUMENT);
} catch (Throwable t) {
Log.d(TAG, t.toString()); // <-------------- THIS THROWS EXCEPTION
}
return null;
}
private InputStream loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
Log.d(TAG, "LoadXML");
InputStream stream = null;
try {
Log.d(TAG, "LoadXML Try " + urlString);
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
stream = conn.getInputStream();
// Log.d(TAG, "stream "+stream.toString());
} finally {
}
return stream;
}
protected void onProgressUpdate(String... values) {
Log.d(TAG, "In ProgressUpdate");
((ArrayAdapter<String>) getListAdapter()).add(values[0]);
}
@Override
protected void onPostExecute(Void result) {
Log.d(TAG, "===================");
Log.d(TAG, "DONE DONE DONE DONE");
}
}
}