我在学习安卓,用Socket实现数据传输。
我看的Android智能穿戴设备开发从入门到精通。服务器端
是java工程,代码如下:
public class AndroidServer implements Runnable {
public void run(){
try{
ServerSocket serverSocket=new ServerSocket(54321);
while(true)
{
System.out.println("等待接收用户连接:");
Socket client=serverSocket.accept();
try
{
BufferedReader in=new BufferedReader(new
InputStreamReader(client.getInputStream()));
String str=in.readLine();
System.out.println("read: "+str);
PrintWriter out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(client.getOutputStream())),true);
out.println("return "+str);
in.close();
out.close();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
client.close();
System.out.println("close");
}
}
}catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread desktopServerThread=new Thread(new AndroidServer());
desktopServerThread.start();
}
}
客户端是安卓工程,布局文件如下:
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="14dp"
android:ems="10" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/editText1"
android:layout_marginRight="14dp"
android:layout_marginTop="67dp"
android:text="发送" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="39dp"
android:text="1" />
一个编辑框、一个按钮、一个文本框。
源代码如下:
public class MainActivity extends ActionBarActivity {
private TextView text1;
private Button but1;
private EditText edit1;
private final String DEBUG_TAG="mySocketAct";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.textView2);
but1=(Button)findViewById(R.id.button1);
edit1=(EditText)findViewById(R.id.editText1);
but1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Socket socket=null;
String mesg=edit1.getText().toString()+"\r\n";
edit1.setText("");
Log.e("dddd", "sent id");
try{
socket =new Socket("192.168.1.101",54321);
PrintWriter out=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream())),true);
out.println(mesg);
BufferedReader br=new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String mstr=br.readLine();
if(mstr!=null)
{
text1.setText(mstr);
}else
{
text1.setText("数据错误");
}
out.close();
br.close();
socket.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(Exception e)
{
Log.e(DEBUG_TAG, e.toString());
}
}
});
}
AndroidManifest.xml添加了权限:
先运行服务器:
再运行客户端,在编辑框里输入文字,点击发送,没反应。
调试了一下,LogCat里打印错误信息:
请高手指教,哪里错了?谢谢!
那两个错误都是你程序里自己打印出来的,而且输出信息不全,无法判断哪里出错。
你安卓程序里用IP是192。168。。,不知道是否是电脑跟自己通信,我电脑跟自己通讯安卓端回环地址是10。0。2。2