所以,我已经为即将发布的新体育应用程序构建了一个Ok RSS阅读器。它将读取各种体育网站的提要并显示它们。我遇到的问题是,在需要互联网连接的活动开始时,Feed会立即加载。
如果互联网连接中断或存在延迟,应用有时会挂起和/或强制关闭我。我需要暂停屏幕,直到从各种XML Feed中提取了Feed内容。
当您点击新的新闻类别时,许多新闻应用和其他RSS阅读器都会这样做。你会得到一个动画“更新”或“加载”对话。这是我目前的代码。
public class MyActivity extends ListActivity implements OnClickListener {
Button btn1;
Button btn2;
Button btn3;
Button btn4;
WebView webview;
TextView feedTitle;
TextView feedDescribtion;
TextView feedPubdate;
TextView feedLink;
private RSSFeed myRssFeed = null;
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listTitle2=(TextView)row.findViewById(R.id.listtitle2);
listTitle2.setText(myRssFeed.getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
String str = (myRssFeed.getList().get(position).getPubdate());
SimpleDateFormat inputFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); //please notice the capital M
SimpleDateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
try {
Date date = inputFormatter.parse(str);
String text = outputFormatter.format(date);
listPubdate.setText(text);
} catch (ParseException e ) {
e.printStackTrace();
}
return row;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hawkcentral);
btn1 = (Button) findViewById(R.id.hawknation_button);
btn1.setOnClickListener(this);
btn2 = (Button) findViewById(R.id.hawkcentral_button);
btn2 .setOnClickListener(this);
btn3 = (Button) findViewById(R.id.hawkeyesports_button);
btn3 .setOnClickListener(this);
espn = (Button) findViewById(R.id.espn_button);
feedTitle = (TextView)findViewById(R.id.feedtitle);
feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
feedPubdate = (TextView)findViewById(R.id.feedpubDate);
feedLink = (TextView)findViewById(R.id.feedlink);
readRss();
}
private void readRss(){
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
feedPubdate.setText("");
feedLink.setText("");
setListAdapter(null);
Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
try {
URL rssUrl = new URL("http://www.sports.com/feed");
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);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (myRssFeed!=null)
{
TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
TextView feedPubdate = (TextView)findViewById(R.id.feedpubDate);
TextView feedLink = (TextView)findViewById(R.id.feedlink);
feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter =
new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(this,HawkCentralDetails.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
intent.putExtras(bundle);
startActivity(intent);
}
答案 0 :(得分:0)
您应该尝试使用 AsyncTask ,这将有助于按照以下链接
http://developer.android.com/reference/android/os/AsyncTask.html
使用AsyncTask,您可以在后台加载数据,并在加载时显示,直到您可以显示ProgressDialog
答案 1 :(得分:0)
你需要在单独的线程中调用readRSS(),因为oncreate会对时间进行限制。您可以使用线程来获取rs并通过将其放入UI线程或使用异步任务来更新UI
指 http://developer.android.com/resources/articles/painless-threading.html
答案 2 :(得分:0)
您可以使用asynctask或使用以下代码使用消息队列来确保仅在从Web请求完全加载内容时向用户显示内容,并且由于其在单独的线程中运行,因此GUI不会暂停以及: InputSource myInputSource;
public void sampleFunction()
{
progressDialog = ProgressDialog.show(this, "", "Getting data...");
new Thread() {
public void run() {
try {
//do the web request to get the feeds
myInputSource = new InputSource(rssUrl.openStream());
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
messageHandler.sendEmptyMessage(0);
// messageHandler.sendEmptyMessage(0);
}
}.start();
}
}
//inside the handler set the string array retrieved from the WS in sampleFunction to the adapter
private Handler messageHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
//here write the code to parse the value stored in myInputSource
}
};