im_cheer
im_cheer
2015-03-09 14:07
采纳率: 50%
浏览 3.0k
已采纳

安卓下建立socket就崩溃,分到子线程仍然不行,求助啊

原代码如下,如果我不用BUTTON去触发的话是可以建立SOCKET的,但是用上BUTTON,把建立SOCKET的方法放到子线程中,仍然exception会显示不能再主线程中使用网络操作,真心求解为什么,看了2天了,刚学JAVA,不太理解。
package com.example.xlguang.wifi;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends ActionBarActivity {
private EditText editText;
private Button button;
private TextView MTextview;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText1);
button = (Button)findViewById(R.id.button1);
final RunnableThread runnable = new RunnableThread();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

        handler.post(runnable);
        }
    });



}

public class RunnableThread implements Runnable
{
public void run() {

        Socket socket = null;
          String message = editText.getText().toString()+ "\r\n" ;
        try {

            //创建客户端socket,注意:不能用localhost或127.0.0.1,Android模拟器把自己作为localhost
            socket = new Socket("172.16.5.30",8000);

            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter
                    (socket.getOutputStream())),true);
            //发送数据
               out.println(message);

            //接收数据
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String msg = in.readLine();
                        if (null != msg){
                            editText.setText(msg);
                            System.out.println(msg);
                        }
                        else{
                            editText.setText("data error");
                        }
             out.close();
              in.close();
        } catch (UnknownHostException e) {
               e.printStackTrace();
        }
        catch (IOException e) {
             e.printStackTrace();
        }
        finally{
            try {
                if (null != socket){
                    socket.close();
                }
            } catch (IOException e) {
                  e.printStackTrace();
            }
        }

}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • danielinbiti
    danielinbiti 2015-03-09 15:37
    已采纳
     handler.post(runnable);  //这是运行在UI线程中的,android会把runnable放到UI线程的消息循环中进行分支处理
    
    

    所以改成

     new Thread(runnable).start();
    
    点赞 评论
  • btz23
    btz23 2016-04-11 15:32

    这个回答真的是太好了,解决了困扰我1天的问题。谢谢danielinbiti和楼主 !!必需留个声!建个新线程就OK啦!

    点赞 评论

相关推荐