android_项目_知识积累_mina通信(android手机登录功能)

   前一篇文章实现了简单的mina服务器端与客户端通信,这篇文章我们要实现android手机上的登录功能。android的应用层语言是java,所以要把前一篇文章的程序改成android版本的也不难。接下来我们来看具体的实现:

  服务器端:

和前面一篇文章的一样就可以了,这里不多说了,详细请看android_项目_知识积累_mina通信(java程序简单通信)。然后这里我们要做的就是把前一篇文章的客户端搬到android手机上来。

        代码如下:

package com.example.android_mina_client;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViewById(R.id.button1).setOnClickListener(
				new android.view.View.OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						final EditText t1 = (EditText) findViewById(R.id.editText1);
						final EditText t2 = (EditText) findViewById(R.id.editText2);
						if ("".equals(t1.getText().toString()) || "".equals(t2.getText().toString())) {
							Toast.makeText(MainActivity.this, "用户名和密码均不能为空!",
									Toast.LENGTH_LONG).show();
						} else {
							// login
							new Thread(){

								@Override
								public void run() {
									// TODO Auto-generated method stub
									super.run();
									login(t1.getText().toString(),t2.getText().toString());
								}
								
							}.start();
						}
					}
				});
	}

	@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;
	}

	private void login(String username,String userpasswd) {
		SocketConnector connector = new NioSocketConnector();
		IoFilter filter = new ProtocolCodecFilter(new TextLineCodecFactory());
		connector.getFilterChain().addLast("gzm", filter);
		SocketAddress soketAddress = new InetSocketAddress("192.168.80.105", 5333);//这里我用的局域网,我的电脑的IP是192.168.80.105

		connector.setHandler(new ClientHandler());
		ConnectFuture future = connector.connect(soketAddress);
		future.join();
		if (!future.isConnected()) {
			System.out.println("连接服务器失败!");
			return;
		}
		future.getSession().write(username+","+userpasswd);
	}
}

其中的ClientHandler类依然不变,和上一篇文章的一样:

package com.example.android_mina_client;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class ClientHandler extends IoHandlerAdapter {
	
	public void messageReceived(IoSession arg0, Object message) throws Exception {
		System.out.println("服务器发来消息:" + message.toString());
	}

	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {

	}
}

整个android工程的结构如下图:

android_项目_知识积累_mina通信(android手机登录功能)_第1张图片

先运行服务器端,然后再运行android客户端,实现的效果如下:

android_项目_知识积累_mina通信(android手机登录功能)_第2张图片

android_项目_知识积累_mina通信(android手机登录功能)_第3张图片

源码下载地址:http://download.csdn.net/detail/guozeming122/6870731

你可能感兴趣的:(android)