7

我正在编写一个程序,该程序从服务器(使用 JSON)从 MySql 获取数据并相应地 更新 UI

我正在使用AsyncTask从服务器获取两种类型的数据

1) Bubble Answers
2) Comments

parseBubbleAnswers 方法成功运行并更新 UI,但 parseComments 类是 AsyncTask,在 doInBackground 中调用 parseComments 方法,没有运行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
            }
        });
    }
}
4

3 回答 3

8

runOnUiThread是 的一个方法ActivityAsyncTask没有引用 Activity。

但是,AsyncTask 已经在 UI 线程上运行,并且设计就是为了做到这一点。

只需处理onPostExecute.

于 2012-12-26T08:47:40.160 回答
8

试试这种方式:

首先创建一个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();
            }
        }
    }

谢谢。

于 2012-12-26T09:15:42.040 回答
0

我遇到了类似的问题。

只需将 Activity 类的引用传递给 parseComments 类即可。

class parseComments extends AsyncTask<String, Integer,String>{

 Activity activity;
 public parseComments(Activity activity){
  this.activity = activity;
 }
}

之后,您可以使用 runOnUiThread 作为

activity.runOnUiThread(new Runnable(){
  @Override
  public void run(){

  }
});

它只适用于 Activity 类。不是上下文类。

于 2019-06-14T14:16:53.733 回答