简体   繁体   中英

How to get RSS feed to work in a fragment in android?

I'm trying to show an RSS feed in a fragment. When you click on a button, the feeds should be pulled and shown in 2 TextViews (Eg = 'tv1' is 'City' and 'tv2' is 'Arsenal').

The code I was using worked fine without fragments.

But when I customized it for a fragment, the app stalls and freezes when you click the button. There are no errors that show when I run the app, but I think there is some issue with parsing the XML from inside the fragment.

Below is the EPLFragment class :

public class EPLFragment extends Fragment {

   String finalUrl="http://www.michael-r-oneill.ie/Custom_Mike/rsseplfixtures.xml";
   HandleXML obj;
   TextView firsttag;
   TextView secondtag;
   ImageButton mykzimagebutton, mykzimagebutton2;   

 public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
 {

View rootView = paramLayoutInflater.inflate(R.layout.epl_layout, paramViewGroup, false);

        this.firsttag = ((TextView)rootView.findViewById(R.id.tv1));
        this.secondtag = ((TextView)rootView.findViewById(R.id.tv2));         
        this.mykzimagebutton = ((ImageButton)rootView.findViewById(R.id.imagebuttonhome));

        this.mykzimagebutton.setOnClickListener(new View.OnClickListener()
        {
        public void onClick(View v)
        {   
            String finalUrl="http://www.michael-r-oneill.ie/Custom_Mike/rsseplfixtures.xml";
            HandleXML obj;

            obj = new HandleXML(finalUrl);
            obj.fetchXML();
            while(obj.parsingComplete);

            firsttag.setText(obj.getFirstTag());
            secondtag.setText(obj.getSecondTag()); 
           }
        });

    return rootView;    
 }

}

And below is the HandleXML class :

 public class HandleXML {

   private String title = "title";
   private String link = "link";
   private String firsttag = "firsttag";
   private String secondtag = "secondtag";
   private String urlString = null;
   private XmlPullParserFactory xmlFactoryObject;
   public volatile boolean parsingComplete = true;
   public HandleXML(String url){this.urlString = url;}
   public String getTitle(){return title;}
   public String getLink(){return link;}    
   public String getFirstTag(){return firsttag;}
   public String getSecondTag(){return secondtag;}

   public void parseXMLAndStoreIt(XmlPullParser myParser) {
   int event;
   String text=null;
   try {
     event = myParser.getEventType();
     while (event != XmlPullParser.END_DOCUMENT) {
     String name=myParser.getName();
     switch (event){
        case XmlPullParser.START_TAG:
        break;
        case XmlPullParser.TEXT:
           text = myParser.getText();
        break;
        case XmlPullParser.END_TAG:
           if(name.equals("title")){
              title = text;
           }
           else if(name.equals("link")){    
              link = text;
           }
           else if(name.equals("firsttag")){
               firsttag = text;
           }
           else if(name.equals("secondtag")){
               secondtag = text;
            }
           else{
           }
           break;
     }       
     event = myParser.next(); 
   }
   parsingComplete = false;
   } catch (Exception e) {
     e.printStackTrace();
  }
  }

 public void fetchXML(){
 Thread thread = new Thread(new Runnable(){
 @Override
 public void run() {
  try {
     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();
     InputStream stream = conn.getInputStream();
     xmlFactoryObject = XmlPullParserFactory.newInstance();
     XmlPullParser myparser = xmlFactoryObject.newPullParser();
     myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
     myparser.setInput(stream, null);
     parseXMLAndStoreIt(myparser);
     stream.close();
  } catch (Exception e) {
  }
  }
  });
  thread.start(); 
  }
  }

Any help is much appreciated! Thanks in advance.

Checking your code, this while seems to be infinite.

while(obj.parsingComplete);

I would need to see your logcat or more info.

I don't recommend you to check concurrency between threads/asyncs with a variable true/false. Why don't you give your method a listener/callback to be call after execution?

An example:

1.Declare an interface with a method to be called after execution.

public interface XMLParserListener{
  //Sample, maybe you have to customize params or return type
  void onParserFinished();
}

2.Make your fragment implement it (or other implementation... it's up to you. I'll make the sample with the implementation in your fragment). This will make you implement onParserFinished().

3.Give yourparser listener to your HandleXML

obj.fetchXML(this);

4.Change your fetchXML implementation and call the callback.

public void fetchXML(XMLParserListener listener){
 Thread thread = new Thread(new Runnable(){
 @Override
 public void run() {
  try {
   ...
   parseXMLAndStoreIt(myparser);
   stream.close();
   listener.onParserFinished();
  } catch (Exception e) {
   ...
...

5.In your onParserFinished() method you can implement whatever you want to do after parsing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM