0

I'm pulling in an rss feed in a new thread (class that implements runnable, shown below) I want to use the strings I get from the rss feed and return them to the main class. The main class simply runs the thread like so

new Thread(new RssParse()).start();

I'm having a really hard time finding ways to return these strings that were cut out of the xml to the main class. Any help is appreciated thanks.

public class RssParse implements Runnable  {

            private static final String MY_DEBUG_TAG = "pfaff";


            public void run(){

            System.out.println("1");
            URL iotd;
            try{

                iotd = new URL("http://www.nasa.gov/rss/image_of_the_day.rss");//set URl
                BufferedReader in;//new BufferedReader
                in = new BufferedReader(new InputStreamReader(iotd.openStream()));//get rss

                XmlPullParserFactory factory;
                factory = XmlPullParserFactory.newInstance();//new factory


                factory.setNamespaceAware(true);
                XmlPullParser xpp;
                xpp = factory.newPullParser();
                xpp.setInput(in);


                int eventType;
                eventType = xpp.getEventType();

                System.out.println(eventType+"!!!!!!!!!!!!!!!!");

            while(eventType!=XmlPullParser.END_DOCUMENT){

                switch(eventType){

                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    String tagName=xpp.getName();
                    System.out.println(tagName+" "+xpp.getDepth());
                    if(tagName.equals("title")&& xpp.getDepth()==4){//depth is specific to this certain rss feed, there are multiple tags with the same names
                        String title=xpp.nextText();// I want to return title and a few other strings but can't figure out how to do this when dealing with threads.
                        System.out.println(title);
                    }

                    break;

                }
                eventType=xpp.next();
            }//switch


                in.close();//close BufferedReader
            } catch (MalformedURLException e){
                e.printStackTrace();
            }catch(XmlPullParserException e1){
                e1.printStackTrace();
            }catch(IOException e2){
                e2.printStackTrace();
            }           
      }//method


}//class
1
  • In Java 5 you would use a Future. See here. Commented Jul 12, 2012 at 0:29

2 Answers 2

4

I'd like to suggest AsyncTask. You have to override doInBackground(Params...) and onPostExecute(Result) of AsyncTask in your sub-class.

Tutorial : Android Threads, Handlers and AsyncTask - Tutorial

Sign up to request clarification or add additional context in comments.

3 Comments

That looks like exactly what I need, looking into it right now, thanks!
Googles example is a little confusing. Do you know of any better ones?
This worked great thanks, can you pass more than one type though? I invoke like the thread like so.... new RssParseSync(this).execute(title,description,date); where all 3 params are strings, any way to pass 3 strings and a bitmap?
0

you can use handler to post it in to main thread

 Runnable runnable = new Runnable() 
 {
  @Override
  public void run() {
   for (int i = 0; i <= 10; i++) {
    handler.post(new Runnable() {
        @Override
            public void run() {
                progress.setProgress(value);
            }
        });
    }
}

};

1 Comment

Inside your run method the object you want to post in to main thread there you can place this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.