2012-12-26 65 views
3

運行我編碼的程序,其從MySQL從服務器提取數據(使用JSON)和它更新UI因此,runOnUiThread不在的AsyncTask

我讀取兩種類型的數據使用的AsyncTask從服務器

1) Bubble Answers 
2) Comments 

的parseBubbleAnswers方法成功運行,並更新UI, 但parseComments類,這是的AsyncTask,並調用parseComments方法d oInBackground,不運行runOnUiThread(new Runnable() { run() });

誰能幫我解決這個

這裏是我的代碼:

public class FetchServer extends Activity 
{ 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     String photoId = "1"; // photo id for which the data is fetched 
     checkBubbleData(photoId); // which call AsyncTask - 2 differnt calls 
    } 
    public void checkBubbleData(String photoId) 
    { 
     new parseBubbleAnswers().execute(photoId); // to fetch bubble answers 
     new parseComments().execute(photoId); // to fetch comments 
    } 
    class parseBubbleAnswers extends AsyncTask<String, Integer,String> 
    { 

     @Override 
     protected String doInBackground(String... params) 
     { 
      // TODO Auto-generated method stub 
      Looper.prepare(); 
      parseBubbleAnswers(); // which has runOnUiThread(new Runnable() which updates (successfully !) the UI 
      return null; 
     } 
    } 
    class parseComments extends AsyncTask<String, Integer,String> 
    { 

     @Override 
     protected String doInBackground(String... params) 
     { 
      // TODO Auto-generated method stub 
      Looper.prepare(); 

      String parseComReturn = parseComments(); 
      if(parseComReturn=="end") 
      { 
       commentBuilder(); // which will update UI after fetch data by parseComments() method 
      } 
     } 
    } 
    public void commentBuilder() 
    { 
     runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error 
     {  
      public void run() 
      { 
       // update UI code 
      } 
     }); 
    } 
} 
+0

檢查我在下面發佈的解決方案。讓我知道是否仍有問題。謝謝。 –

回答

6

試試這樣:

首先創建一個Handler

Handler mHandler = new Handler(); 

改變這一狀況,

public void commentBuilder() 
    { 
     runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error 
     {  
      public void run() 
      { 
       // update UI code 
      } 
     }); 
    } 

有了,

public void commentBuilder() 
    { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       while (isRunning) { 
        try { 
         // Thread.sleep(10000); 
         mHandler.post(new Runnable() { 

          @Override 
          public void run() { 
           // TODO Auto-generated method stub 
           // Write your code here to update the UI.        
          } 
         }); 
        } catch (Exception e) { 
         // TODO: handle exception 
        } 
       } 
      } 
     }).start(); 
    } 

停止線程通過這一次你與UI做,

isRunning = false; 

編輯:

嘗試使用異步任務是這樣的:

class parseComments extends AsyncTask<String, Integer,String> 
    { 
     protected String doInBackground(String... params) { 
      String parseComReturn = parseComments(); 
      return parseComReturn; 
     } 

     protected void onPostExecute(String result) { 
      if(result.equals("end")) 
      { 
       commentBuilder(); 
      } 
     } 
    } 

感謝。

+0

我使用這一個,即時通訊也調試代碼,當調試器來到})。start();這行,它在方法結束時沒有任何錯誤,它假設進入run(),但不執行run(),爲什麼? – SML

+0

@SunilLohar你是否聲明'boolean isRunning = true;'在應用程序的頂部?因爲它對我來說工作得很好。 –

+0

沒有它不適合我,我已經聲明布爾isRunning = true;但是主要的問題是,它不會在run()內部,在start()之後,它會在調用該方法的AsyncTask中執行return語句。 – SML

5

runOnUiThreadActivity的方法,AsyncTask沒有提及活動。

但是,AsyncTask已經在UI線程上運行,並被設計爲完成該操作。

只處理onPostExecute中的UI更改。

+0

保護無效onPostExecute(字符串結果){}方法沒有執行 – SML

+0

你需要返回它執行'doInBackground'完成後,嘗試覆蓋它並記錄,看看它是否被調用 – thepoosh