Android socket通信,数据读取。

public class ChatRoom extends ActionBarActivity {
protected TextView tv_chat_show;
protected EditText ed_chat_input;
protected Button btn_send;
protected ActionBarActivity that;
protected String receiverMsg;
protected String sendStr;
protected Socket socket;
protected boolean flag;
protected boolean isClicked;

    // 创建handle 对象处理接受数据
Handler myHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        tv_chat_show.append("server: " + msg.obj.toString() + "\n");
        Log.i("show", msg.obj.toString());
    }
};

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chat_room);

    tv_chat_show = (TextView) findViewById(R.id.tv_chat_show);
    ed_chat_input = (EditText) findViewById(R.id.ed_chat_input);
    btn_send = (Button) findViewById(R.id.btn_send);
    that= this;
    // 套接字是否存在标志
    flag = false;
            // 按钮是否点击标志
    isClicked = false;

            // 建立线程连接socket
    new Thread(){
        @Override
        public void run() {
            super.run();
            socket = new Socket();
            try {
                socket.connect(new InetSocketAddress("10.1.11.38", 443), 5000);
                flag = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();

            // 创建套接字发送线程
    Thread threadSend = new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                                    // 等待1秒确保套接字建立完成
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DataOutputStream dos;
                            //套接字存在并且点击发送按钮
            while(flag) {
                if(isClicked) {
                    try {
                        dos = new DataOutputStream(socket.getOutputStream());
                        dos.writeUTF(sendStr);
                        isClicked = false;
                        dos.close();
                        Toast.makeText(that,"发送" + sendStr,Toast.LENGTH_SHORT).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    };
            // 创建套接字接受线程
    Thread threadRev = new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            DataInputStream dis;
            while(flag){
                try {
                    dis = new DataInputStream(socket.getInputStream());
                    receiverMsg = dis.readUTF();
                    Log.i("rev",receiverMsg);
                    Message rM = new Message();
                    rM.obj = receiverMsg;
                    myHandler.sendMessage(rM);
                    dis.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    };
    threadSend.start();
    threadRev.start();
    btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendStr = ed_chat_input.getText().toString().trim();
            ed_chat_input.setText(null);
            tv_chat_show.append("Client: " + sendStr + "\n");
            Log.i("isClicked", "点击");
            isClicked = true;
        }
    });
}

}
测试Log.i("rev",receiverMsg);能够接收到服务器传回来的消息,但是点击发送后软件会崩溃,提示 unfortunately,**** is stoped;
Android studio报错信息:
12-22 10:57:34.290 17263-17797/com.teemo.oneteemo E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-931
Process: com.teemo.oneteemo, PID: 17263
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:200)
at android.os.Handler.(Handler.java:114)
at android.widget.Toast$TN.(Toast.java:342)
at android.widget.Toast.(Toast.java:99)
at android.widget.Toast.makeText(Toast.java:248)
at com.teemo.oneteemo.ChatRoom$3.run(ChatRoom.java:109)

4个回答

在sendmessage这个线程里面加个Looper.prepare()试试

在public void run() 方法开始的时候加上Looper.prepare();

Toast.makeText(that,"发送" + sendStr,Toast.LENGTH_SHORT).show();

是这句错了,不要在线程中这么用

Looper.prepare();
Toast.makeText(that,"发送" + sendStr,Toast.LENGTH_SHORT).show();
  Looper.loop();

或者这么写

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问