这是客户端:
package com.example.android_client2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.widget.EditText;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Android_Client2 extends Activity {
private TextView myTextView;
private EditText myEditText;
private Button btnProduct=null;
// private OutputStream oi;
DataOutputStream out;
private String edit;
private Socket sk;
boolean flag = false;
Thread thread;
Button.OnClickListener button_listener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android__client2);
myTextView = (TextView) findViewById(R.id.textView1);
myEditText = (EditText) findViewById(R.id.edittext);
btnProduct = (Button) findViewById(R.id.button1);
btnProduct.setOnClickListener(listener);
Thread t = new Thread(new Runnable() {
public void run() {
try {
sk = new Socket("127.0.0.1", 12345); // 创建客户端数据输入,输出流,用于对服务器发送或接收数据
InputStream is = sk.getInputStream();
byte[] b = new byte[1024];
int leng = is.read(b);
String msg = new String(b);
myTextView.setText(msg);
String ss = new String(b, 0, leng, "UTF-8");
System.out.println(ss);
is.close();
sk.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
(new Thread() {// 这里有一个新线程,网络操作放到这里线程里
public void run() {
super.run();
try {
// sk = new Socket("192.168.253.1", 12345);
// 创建客户端数据输入,输出流,用于对服务器发送或接收数据
edit = myEditText.getText().toString();// 得到EditText里面的内容
out = new DataOutputStream(sk.getOutputStream());
out.writeUTF(edit);
} catch (IOException e1) {
System.out.println("无法连接");
}
}
}).start();
}
};
}
这是服务器端
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Server2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Server2 a = new Server2();
a.dopost();
}
public void dopost() {
try {
ServerSocket s = new ServerSocket(12345);
String mse;
String temp=null;
Scanner sc = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
Socket c = s.accept();
String recever ;
System.out.println(c.getInetAddress().getHostAddress());
InputStream in = c.getInputStream();
OutputStream oi = c.getOutputStream();
//从安卓端接受数据
byte[] b = new byte[1024];
int length = in.read(b);
String msg = new String(b);
String ss = new String(b, 0, length);
System.out.println(ss);
System.out.println("33333333333333");
//向安卓端发送数据
if(!(sc.next()).equals(temp))
{
recever = sc.next();
oi.write(recever.getBytes("UTF-8"));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后运行后程序直接退出,ip改成自己的ip试过了,也崩了
------解决思路----------------------
楼主的
------解决思路----------------------
public void run() {
try {
sk = new Socket("127.0.0.1", 12345); // 创建客户端数据输入,输出流,用于对服务器发送或接收数据
InputStream is = sk.getInputStream();
byte[] b = new byte[1024];
int leng = is.read(b);
String msg = new String(b);
//myTextView.setText(msg);
handler.hasMessages(1, msg);//新增
String ss = new String(b, 0, leng, "UTF-8");
System.out.println(ss);
is.close();
sk.close();
} catch (Exception e) {
e.printStackTrace();
}
}
下面再增加一个
Handler handler=new Handler(new Handler.Callback()
{
@Override
public boolean handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
myTextView.setText(msg.obj);
break;
default:
break;
}
return false;
}
});
------解决思路----------------------
刚才写错了
// myTextView.setText(msg);
Message myMsg = handler.obtainMessage(1,msg);
handler.sendMessage(myMsg);
------解决思路----------------------
没崩溃是你button添加的监听是null吧
btnProduct.setOnClickListener(listener);//添加了监听
private OnClickListener listener;//但是监听是空。。。。
你添加了Null的listener,之后才在thread里创建了listener对象
第65行。。。你依然myTextView.setText(msg);//在线程里才做ui线程。。。会报错的
使用handler……百度一下handler的使用就明白怎么用了