安卓开发 监听socket代码,为什么放到新线程里就不行呢?
可以用的代码:publicvoidonCreate(BundlesavedInstanceState){。。。。。。。。(度娘嫌长,删掉冗余代码)try{//向服务器发送...
可以用的代码:public void onCreate(Bundle savedInstanceState) {。。。。。。。。(度娘嫌长,删掉冗余代码)try {//向服务器发送信息PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);out.println(aa);out.flush();
//************接受服务器的信息,这样放置是可以的BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));String mstr=new String(br.readLine().getBytes(),"utf-7");if(mstr!=null){text1.setText(mstr);}else{text1.setText("数据错误");}//**************接受信息代码到此} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}catch(Exception e){Log.e(DEBUG_TAG,e.toString());}} });}加入新线程,出错的代码://**********************此处建立了新线程public class receivethread extends Thread{public void run(){while(true){try {BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));String mstr = new String(br.readLine().getBytes(),"utf-7");if(mstr!=null){text1.setText(mstr);}else{text1.setText("数据错误");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//***************监听线程 到此结束public void onCreate(Bundle savedInstanceState) {。。。。。删除冗余代码try {socket = new Socket("192.168.10.5",545);text1.setText("连接成功了");receivethread mReceiveThread = new receivethread(); //这里调用监听的进程//stop = false;//开启线程mReceiveThread.start();} catch (UnknownHostException e2) {// TODO Auto-generated catch blocktext1.setText("连接错误1");e2.printStackTrace();} catch (IOException e2) {// TODO Auto-generated catch blocktext1.setText("时钟超时,未连接");e2.printStackTrace();}// text1.setText("连接成功");// 删除冗余代码}
怎么排版都没了 展开
//************接受服务器的信息,这样放置是可以的BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));String mstr=new String(br.readLine().getBytes(),"utf-7");if(mstr!=null){text1.setText(mstr);}else{text1.setText("数据错误");}//**************接受信息代码到此} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}catch(Exception e){Log.e(DEBUG_TAG,e.toString());}} });}加入新线程,出错的代码://**********************此处建立了新线程public class receivethread extends Thread{public void run(){while(true){try {BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));String mstr = new String(br.readLine().getBytes(),"utf-7");if(mstr!=null){text1.setText(mstr);}else{text1.setText("数据错误");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//***************监听线程 到此结束public void onCreate(Bundle savedInstanceState) {。。。。。删除冗余代码try {socket = new Socket("192.168.10.5",545);text1.setText("连接成功了");receivethread mReceiveThread = new receivethread(); //这里调用监听的进程//stop = false;//开启线程mReceiveThread.start();} catch (UnknownHostException e2) {// TODO Auto-generated catch blocktext1.setText("连接错误1");e2.printStackTrace();} catch (IOException e2) {// TODO Auto-generated catch blocktext1.setText("时钟超时,未连接");e2.printStackTrace();}// text1.setText("连接成功");// 删除冗余代码}
怎么排版都没了 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询