【问题标题】:How to return values from a thread如何从线程返回值
【发布时间】:2012-07-10 14:35:59
【问题描述】:

我在一个新线程(实现可运行的类,如下所示)中拉入一个 rss 提要,我想使用从 rss 提要中获得的字符串并将它们返回给主类。主类只是像这样运行线程

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

我很难找到将这些从 xml 中剪切出来的字符串返回给主类的方法。感谢任何帮助。

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

【问题讨论】:

  • 在 Java 5 中,您将使用 Future。见here

标签: java android multithreading return return-value


【解决方案1】:

我想推荐AsyncTask。您必须在子类中覆盖 AsyncTask 的 doInBackground(Params...)onPostExecute(Result)

教程:Android Threads, Handlers and AsyncTask - Tutorial

【讨论】:

  • 这看起来正是我需要的,正在研究它,谢谢!
  • Google 的例子有点令人困惑。你知道更好的吗?
  • 这很好,谢谢,但是你能传递多个类型吗?我像这样调用线程...... new RssParseSync(this).execute(title,description,date);其中所有 3 个参数都是字符串,有什么方法可以传递 3 个字符串和位图?
【解决方案2】:

您可以使用处理程序将其发布到主线程

 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);
            }
        });
    }
}

};

【讨论】:

  • 在你的 run 方法中,你想要发布到主线程的对象,你可以放置这个代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多