写了个android与pc端通信的例子,pc端用的是mfc写的,做服务端。想实现以下功能:
android填写完用户名和密码点击登录按钮,android把用户名和密码打包发给pc端,pc端收到后在发出一条消息给android,android收到并解析这条消息后启动一个新Activity。
现在的情况是android能给PC发信息,PC端给android发信息收不到。
这是android的源码:
package com.example.sendtoserver;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.os.Message;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
Socket socket=null;
String ip="192.168.137.1";
int port=8888;
BufferedReader br=null;
BufferedWriter bw=null;
Button loginButton=null;
EditText username=null;
EditText password=null;
ReceiveThread receThread=null;
String receString=null;
InitThread it=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
loginButton=(Button)this.findViewById(R.id.login);
loginButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(bw!=null)
{
try
{
bw.write("/Cmd Login /username "+username.getText().toString()+" /password "+password.getText().toString()+" /");
bw.flush();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
username=(EditText)this.findViewById(R.id.username);
password=(EditText)this.findViewById(R.id.password);
it=new InitThread();
it.start();
receThread=new ReceiveThread();
receThread.start();
}
class InitThread extends Thread
{
@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
try
{
socket=new Socket(ip,port);
bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ReceiveThread extends Thread
{
@Override
public void run()
{
// TODO Auto-generated method stub
super.run();
if(br!=null)
{
try
{
int count=0;
char[] buffer=null;
while(true)
{
if((count=br.read(buffer))>0)
{
break;
}
}
receString=buffer.toString();
String[] cmd=parse(receString);
Intent intent=new Intent();
intent.putExtra("id",(cmd[2].split(" "))[1]);
intent.putExtra("queueId",(cmd[3].split(" "))[1]);
intent.putExtra("queueName",(cmd[4].split(" "))[1]);
intent.setClass(MainActivity.this,ListActivity.class);
(MainActivity.this).startActivity(intent);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String[] parse(String rece)
{
String[] elem=null;
elem=rece.split("/");
return elem;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
if(socket!=null)
{
try
{
socket.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null)
{
try
{
br.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null)
{
try
{
bw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// it.destroy();
// receThread.destroy();
}
}
------解决思路----------------------
我建议你在 发送数据后启动ReceiveThread 确保你的Socket连接已经成功,考虑网络连接可能有延时。
bw.flush();下面发送后,开始尝试接收数据。
if((count=br.read(buffer))>0)
{
break;
}
这个地方你写读取数据到buffer ,可能无法跳出去,
你这样写
if((count=br.read(buffer))>0)
{
break;
//break 直接启动Activity, 试试。
}
------解决思路----------------------
粗略看了一下你的代码,网络操作最好放在线程里,你按钮单机就对Socket读写(也就是说在ui线程里操作了),在Android4.0以上是不能执行的,把他们放在线程中吧,不过这就要考虑线程里怎么更新ui了,很久没写Android了,应该是这样的,你试一下!
------解决思路----------------------
楼主,java最牛逼哄哄的设计模式呢?你是不是应该将通信的底层从界面里面抽出来啊?