runOnUiThread未在AsyncTask中运行

SML*_*SML 6 multithreading android

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

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

1) Bubble Answers
2) Comments
Run Code Online (Sandbox Code Playgroud)

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
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*rma 8

试试这种方式:

首先创建一个Handler:

Handler mHandler = new Handler();
Run Code Online (Sandbox Code Playgroud)

改变这个,

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
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

随着,

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();
    }
Run Code Online (Sandbox Code Playgroud)

完成UI后,停止线程,

isRunning = false;
Run Code Online (Sandbox Code Playgroud)

编辑:

尝试以这种方式使用异步任务:

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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

谢谢.


the*_*osh 7

runOnUiThread是一种方法Activity,AsyncTask没有参考活动.

但是,AsyncTask已经在UI线程上运行,并且被设计为完全正确.

只是处理UI的变化onPostExecute.