Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓系统:在RSS源从站点中取出之前暂停_Android_Rss_Toast_Feed - Fatal编程技术网

Android 安卓系统:在RSS源从站点中取出之前暂停

Android 安卓系统:在RSS源从站点中取出之前暂停,android,rss,toast,feed,Android,Rss,Toast,Feed,因此,我为即将发布的新体育应用程序构建了一个Ok RSS阅读器。它将读取来自各种体育网站的提要并显示它们。我遇到的问题是,提要在需要internet连接的活动开始时立即加载 如果互联网连接中断或出现延迟,应用程序有时会挂起和/或强制关闭。我需要一些东西暂停屏幕,直到从各种XML提要中提取提要内容 当你点击一个新的新闻类别时,许多新闻应用程序和其他RSS阅读器都会这样做。您将获得一个动画更新或加载对话框。这是我目前的代码 public class MyActivity extends ListAc

因此,我为即将发布的新体育应用程序构建了一个Ok RSS阅读器。它将读取来自各种体育网站的提要并显示它们。我遇到的问题是,提要在需要internet连接的活动开始时立即加载

如果互联网连接中断或出现延迟,应用程序有时会挂起和/或强制关闭。我需要一些东西暂停屏幕,直到从各种XML提要中提取提要内容

当你点击一个新的新闻类别时,许多新闻应用程序和其他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);
}
您应该尝试使用AsyncTask,它将有助于遵循以下链接


使用AsyncTask,您可以在后台加载数据,并在加载时显示,直到您可以显示ProgressDialog时为止。您需要在单独的线程中调用readRSS,因为oncreate附带了时间限制。您可以使用线程获取rss并通过将其放入UI线程或使用异步任务更新UI

提及

您可以使用asynctask,也可以使用以下使用消息队列的代码,以确保内容仅在从web请求完全加载时才显示给用户,并且由于其在单独的线程中运行,因此GUI也不会暂停: 输入源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 

        }
    };

谢谢这是一个很好的答案,从我最初的教程中,我找到并得到了我所需要的。我理解您在这里所说的,但对于大多数Java新手来说,我仍然需要了解如何实现它。我仍在尝试对所有Java修饰符进行体验。我在一周内学到了很多东西,但还有很长的路要走。