在博客中有登陆界面的文章http://blog.csdn.net/vestigge/article/details/8124674
就不在重复了,直接看登陆的代码,
用Mina传递字符串上节已经看过了,要实现传递对象,也非常简单,只需要修改一下过滤器:
chain.addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
在Android客户端,登陆的Activity中:
public class LoginActivity extends Activity{
private EditText accountEditText;
private EditText passwordEditText;
private CheckBox remeberCheckBox;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_login);
accountEditText=(EditText) findViewById(R.id.login_account);
passwordEditText=(EditText) findViewById(R.id.login_password);
remeberCheckBox=(CheckBox) findViewById(R.id.login_remember);
findViewById(R.id.login_login).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(accountEditText.getText().toString().equals("") ||
passwordEditText.getText().toString().equals("")){
Toast.makeText(LoginActivity.this, "账号或密码不能为空!", Toast.LENGTH_SHORT).show();
}else{
User user=new User();
user.setAccount(Integer.parseInt(accountEditText.getText().toString()));
user.setPassword(passwordEditText.getText().toString());
user.setOperation(VQMessageType.LOGIN);
boolean b=new VQClient().sendLoginInfo(user);
//如果登录成功
if(b){
Toast.makeText(LoginActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
}else{
Toast.makeText(LoginActivity.this, "连接超时,登陆失败!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
可以看到会调用VQClient这个类中的方法去发送登陆请求,VQClient的实现:
public class VQClient {
private static int PORT=5469;
public boolean sendLoginInfo(User u){
boolean b=false;
System.setProperty("java.net.preferIPv6Addresses", "false");//2.2会有ipv6的问题,
SocketConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(300000);
DefaultIoFilterChainBuilder chain=connector.getFilterChain();
chain.addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
SocketSessionConfig cfg = connector.getSessionConfig();
cfg.setUseReadOperation(true);
IoSession session = connector.connect(new InetSocketAddress("10.0.2.2", PORT))
.awaitUninterruptibly().getSession();
//发送并等待完成
session.write(u).awaitUninterruptibly();
//接收
ReadFuture readFuture = session.read();
//接收超时
if (readFuture.awaitUninterruptibly(30000,TimeUnit.SECONDS)) {
VQMessage message = (VQMessage) readFuture.getMessage();
//如果是登陆成功的信息
if(message.getType().equals(VQMessageType.SUCCESS)){
b= true;
}
} else {
b= false;
}
//断开连接
session.close(true);
session.getService().dispose();
return b;
}
}
然后服务器端变化不大,在收到登陆请求后,会查询数据库,返回一个信息,来表明登陆成功或者失败。
public void messageReceived(IoSession session, Object message)
throws Exception {
User user=(User) message;
if(user.getOperation().equals(VQMessageType.LOGIN)){
//操作数据库
boolean b=new UserDao().login(user.getAccount(), user.getPassword());
if(b){
System.out.println(">> ["+user.getAccount()+"] 上线了");
//告诉客户端登陆成功
VQMessage m=new VQMessage();
m.setType(VQMessageType.SUCCESS);
session.write(m);
}
}
}
最后附上测试效果: