2010-08-06 56 views
0

我已經實現了一個定時器,每15分鐘解析一個URL(Timer任務)。 我創建的對象獲取該數據。之後,我使用它在屏幕上顯示數據。從可運行/ TimerTask檢索字符串

現在,無論何時我嘗試從runnable中檢索到Object/a String = Object.toString(),我都會得到空指針異常和致命錯誤。

我的問題是是否有可能使用其他技術來完成它,或者Object停止存在於runnable之外,並且我們可以做的事情不多;如果是這種情況,任何人都可以告訴我是否有另一種實現定時器/可運行的方式?

非常感謝

這裏是我的大部分代碼,我有一個問題

@Override 
    public void onCreate(Bundle icicle) { 
      super.onCreate(icicle) 
      final TextView tv = new TextView(this); 
      TimerTask scanTask; 
      final Handler handler = new Handler(); 
      Timer t = new Timer();     
       scanTask = new TimerTask() { 
        public void run() { 
          handler.post(new Runnable() { 
            public void run() { 
             URL url = null; 
             try { 
              url = new URL("http://www.eurosport.fr/"); 
             } catch (MalformedURLException e3) { 

              e3.printStackTrace(); 
             } 

             /* Get a SAXParser from the SAXPArserFactory. */ 
             SAXParserFactory spf = SAXParserFactory.newInstance(); 
             SAXParser sp; 
             try { 
              sp = spf.newSAXParser(); 
             } catch (ParserConfigurationException e2) { 

              e2.printStackTrace(); 
             } catch (SAXException e2) { 

              e2.printStackTrace(); 
             } 

             /* Get the XMLReader of the SAXParser we created. */ 
             XMLReader xr = null; 
             try { 
              sp = spf.newSAXParser(); 
              xr = sp.getXMLReader(); 
             } catch (SAXException e1) { 

              e1.printStackTrace(); 
             } catch (ParserConfigurationException e) { 

              e.printStackTrace(); 
             } 
             /* Create a new ContentHandler and apply it to the XML-Reader*/ 
             ExampleHandler myExampleHandler = new ExampleHandler(); 
             try { 
              sp = spf.newSAXParser(); 
             } catch (ParserConfigurationException e1) { 
              // TODO Auto-generated catch block 
              e1.printStackTrace(); 
             } catch (SAXException e1) { 
              // TODO Auto-generated catch block 
              e1.printStackTrace(); 
             } 
             xr.setContentHandler(myExampleHandler); 

             /* Parse the xml-data from our URL. */ 
             try { 
              xr.parse(new InputSource(url.openStream())); 
             } catch (IOException e) { 

              e.printStackTrace(); 
             } catch (SAXException e) { 

              e.printStackTrace(); 
             } 
             /* Parsing has finished. */ 

             /* Our ExampleHandler now provides the parsed data to us. */ 
             ParsedExampleDataSet parsedExampleDataSet = 
                         myExampleHandler.getParsedData(); 

             System.out.println(parsedExampleDataSet.toString()); 

             tv.setText(parsedExampleDataSet.toString()); 


            Context context = this.getBaseContext(); 

// I also dont understand why inside the runnable getBaseContext() does not exist ??? 

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), 
      R.raw.nature1) 
     context.setWallpaper(mBitmap); 


            } 


          }); 

        } }; 
        // I want to retrieve ParsedExampleDataSEt here in order to use it is it Possible ???? 



        this.setContentView(tv); 



        long temps=1*15*1000; 

       t.scheduleAtFixedRate(scanTask, 300,temps); 
+1

你能顯示代碼嗎? – 2010-08-06 14:06:48

+1

顯示你的一些代碼並解釋你在服務器端Servlets中使用的是什麼?或者是什麼? (Servlet本身就是線程)。如果你提到這些東西,人們很容易回答:) – Paul 2010-08-06 14:07:39

+1

使用服務和廣播 – Falmarri 2010-08-06 17:30:02

回答

2

潛在醜方法: 擴展的TimerTask,使抽象的方法,如

public abstract void onUrlRetrivalFinished(String data); 

當您創建TimerTask對象時,您現在可以創建該方法的匿名實現,並在該方法中處理檢索到的數據。

的(在我看來)少醜方法:

做一個接口,如:

public interface UrlRetrivalListener { 
    public void onUrlRetrivalFinished(String data); 
} 

子類的TimerTask並作出領域,如:

private UrlRetrivalListener listener; 

現在實現上面提到的監聽器接口,在其中處理檢索到的String。將偵聽器作爲參數傳遞給TimerTask,或者讓TimerTask擁有多個偵聽器,並在檢索/解析所需的數據時,只需在UrlRetrivalFinished()方法上調用偵聽器即可。

這應該可以做到,但是更多的信息會更好。

+0

我剛加入我的大部分代碼。我可以獲取數據並將其顯示在屏幕上。但我無法在可運行的外部使用它。如果您覺得我的代碼不夠好,請隨時編輯並給我一些反饋,因爲我是Android新手!乾杯 我在代碼中添加了我的主要問題;) – Amine 2010-08-08 15:17:39