如何处理AsyncTask与UI线程在做的情况下

时间:2014-08-22 16:26:00

标签: android multithreading sockets android-asynctask

我正在用android编写一个socket客户端应用程序。现在我遇到了如何在do while循环中处理我的嵌套UI的问题。因为我希望我的程序从服务器继续监听消息,直到服务器终止。因为网络异常,我将这些套接字连接放入AsyncTask,但是一旦我运行客户端,它只能处理processConnection()中的readobject()

这是原始客户端代码。

package com.example.socketclien;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ClientFragment extends Fragment {

    private Socket clientSocket;
    private Button mSentButton;
    private TextView mMessageTextView;
    private EditText mMessagEditText;
    private String message="";
    private ObjectOutputStream outputStream;
    private ObjectInputStream inputStream;
    private static String TAG="clientFragment";
    AsyncTask<Void, Void, Void> mRegisterTask;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.i(TAG,"onCreate starts");

    }
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
    {
        //refer a view which wires a layout
        View v = inflater.inflate(R.layout.fragment_client,parent,false);

    mSentButton=(Button)v.findViewById(R.id.Sentbutton);
    mMessageTextView=(TextView)v.findViewById(R.id.messageTextView);
    Log.i(TAG,"onCreateView starts");
    mMessagEditText=(EditText)v.findViewById(R.id.Message);
     new connection().execute();

    return v;
    }

    public void runClient() throws IOException, Throwable
    {
        Log.i(TAG,"runClient() starts");
        connectToServer();
        getStreams();
        processConnection();
        closeConnection();
    }




    private void connectToServer() throws IOException, Exception {
        // TODO Auto-generated method stub
        Log.i(TAG,"connectToServer() starts");
        mMessageTextView.setText("Attempting connection\n");
        Log.i(TAG,"connectToServer() 's setText starts");
        clientSocket = new Socket("134.129.125.123",8080);
        Log.i(TAG,"connectToServer() 's Connect to 134.129.125.123");
        mMessageTextView.append("Connect to "+"134.129.125.123 \n");
    }


    private void getStreams() throws IOException{
        // TODO Auto-generated method stub
        Log.i(TAG,"getStreams()'s onGetStream starts");
        outputStream = new ObjectOutputStream(clientSocket.getOutputStream());
        outputStream.flush();
        Log.i(TAG,"getStreams()'s outputStream created");
        inputStream = new ObjectInputStream(clientSocket.getInputStream());
        mMessageTextView.append("\nGot i/O stream \n");

    }

    private class connection extends AsyncTask{

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            try {
                connectToServer();
                getStreams();
                processConnection();
                closeConnection();
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }



    }
    private void processConnection(){

        Log.i(TAG,"ProcessConnection() starts");
        // TODO Auto-generated method stub
        do {


                try {
                    message=(String) inputStream.readObject();
                } catch (OptionalDataException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.i(TAG,"ProcessConnection()'s readobject() starts");

                mMessageTextView.append("\n"+ message);
                Log.i(TAG,"ProcessConnection()'s append() starts");


        } while (!message.equals("SERVER>>> TERMINATE"));
    }


    private void closeConnection() throws IOException {
        // TODO Auto-generated method stub
        Log.i(TAG,"closeConnection starts");
        mMessageTextView.append("\nTerminating connection\n");

        outputStream.close();
        inputStream.close();
        clientSocket.close();
    }

    private void sentDate(String message)
    {
        try {
            outputStream.writeObject("CLIENT>>>"+message);
            outputStream.flush();
            mMessageTextView.append("\nCLIENT>>>"+ message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

错误:

08-22 11:11:53.230: W/System.err(12048): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-22 11:11:53.240: W/System.err(12048):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
08-22 11:11:53.240: W/System.err(12048):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853)
08-22 11:11:53.240: W/System.err(12048):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
08-22 11:11:53.240: W/System.err(12048):    at android.view.View.invalidate(View.java:10935)
08-22 11:11:53.240: W/System.err(12048):    at android.view.View.invalidate(View.java:10890)
08-22 11:11:53.240: W/System.err(12048):    at android.widget.TextView.updateAfterEdit(TextView.java:7430)
08-22 11:11:53.240: W/System.err(12048):    at android.widget.TextView.handleTextChanged(TextView.java:7453)
08-22 11:11:53.240: W/System.err(12048):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9187)
08-22 11:11:53.240: W/System.err(12048):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
08-22 11:11:53.240: W/System.err(12048):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
08-22 11:11:53.240: W/System.err(12048):    at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:253)
08-22 11:11:53.240: W/System.err(12048):    at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
08-22 11:11:53.240: W/System.err(12048):    at android.widget.TextView.append(TextView.java:3409)
08-22 11:11:53.240: W/System.err(12048):    at android.widget.TextView.append(TextView.java:3396)
08-22 11:11:53.240: W/System.err(12048):    at com.example.socketclien.ClientFragment.processConnection(ClientFragment.java:174)
08-22 11:11:53.240: W/System.err(12048):    at com.example.socketclien.ClientFragment.access$2(ClientFragment.java:153)
08-22 11:11:53.240: W/System.err(12048):    at com.example.socketclien.ClientFragment$connection.doInBackground(ClientFragment.java:100)
08-22 11:11:53.240: W/System.err(12048):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-22 11:11:53.240: W/System.err(12048):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-22 11:11:53.240: W/System.err(12048):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-22 11:11:53.240: W/System.err(12048):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-22 11:11:53.240: W/System.err(12048):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-22 11:11:53.240: W/System.err(12048):    at java.lang.Thread.run(Thread.java:841)
08-22 11:15:51.574: W/System.err(13286): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-22 11:15:51.574: W/System.err(13286):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
08-22 11:15:51.574: W/System.err(13286):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853)
08-22 11:15:51.574: W/System.err(13286):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
08-22 11:15:51.574: W/System.err(13286):    at android.view.View.invalidate(View.java:10935)
08-22 11:15:51.574: W/System.err(13286):    at android.view.View.invalidate(View.java:10890)
08-22 11:15:51.574: W/System.err(13286):    at android.widget.TextView.updateAfterEdit(TextView.java:7430)
08-22 11:15:51.584: W/System.err(13286):    at android.widget.TextView.handleTextChanged(TextView.java:7453)
08-22 11:15:51.584: W/System.err(13286):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9187)
08-22 11:15:51.584: W/System.err(13286):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
08-22 11:15:51.584: W/System.err(13286):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
08-22 11:15:51.584: W/System.err(13286):    at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:253)
08-22 11:15:51.584: W/System.err(13286):    at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:30)
08-22 11:15:51.584: W/System.err(13286):    at android.widget.TextView.append(TextView.java:3409)
08-22 11:15:51.584: W/System.err(13286):    at android.widget.TextView.append(TextView.java:3396)
08-22 11:15:51.584: W/System.err(13286):    at com.example.socketclien.ClientFragment.processConnection(ClientFragment.java:131)
08-22 11:15:51.584: W/System.err(13286):    at com.example.socketclien.ClientFragment.access$2(ClientFragment.java:110)
08-22 11:15:51.584: W/System.err(13286):    at com.example.socketclien.ClientFragment$connection.doInBackground(ClientFragment.java:98)
08-22 11:15:51.584: W/System.err(13286):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-22 11:15:51.584: W/System.err(13286):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-22 11:15:51.584: W/System.err(13286):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-22 11:15:51.584: W/System.err(13286):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-22 11:15:51.584: W/System.err(13286):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-22 11:15:51.584: W/System.err(13286):    at java.lang.Thread.run(Thread.java:841)

我知道它必须在UIthread和Asytask之间发生。

2 个答案:

答案 0 :(得分:1)

您无法从无UI线程更新UI线程或任何窗口小部件。 asynctask是一个无UI线程,因此您无法从textviewonPreExecute()onProgressUpdate中的方法执行更新onPostExecute()。每次您想要访问UI时您可以使用runOnUiThread线程,例如doInBackground方法中的asynctask中的所有代码都可以像以下任何方式一样访问mMessageTextView

 ClientFragment.this.getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mMessageTextView.append("\nTerminating connection\n");
                    }
                });

或者您可以使用:

   mMessageTextView.post(new Runnable() {

    @Override
    public void run() {
       mMessageTextView.append("\nTerminating connection\n");
    }
});

您还可以使用publishProgress()onProgressUpdate发布更新。有关快速信息,请查看:

http://developer.android.com/reference/android/os/AsyncTask.html

您也可以使用handler。如果您想了解更多,请让我编辑此答案以包含处理程序示例代码。

答案 1 :(得分:0)

您无法从doInBackground()方法更新用户界面。

您可以致电Activity.runOnUiThread(Runnable action)例如

getActivity().runOnUiThread(new Runnable() {

    @Override
    public void run() {
        mMessageTextView.append("Connect to "+"134.129.125.123 \n");
    }
});

或使用此:

mMessageTextView.post(new Runnable() {

    @Override
    public void run() {
        mMessageTextView.append("Connect to "+"134.129.125.123 \n");
    }
});