在android使用mina实现网络访问
mina是java基中nio的网络访问框架,放入android时会出现一些问题,一是SLF4J包不支持android,需要下载android版,另外是MINA 2.0.X中很多类改了包名。
一、下载jar包,添加到android项目引用
1.MINA 2.0.4 Core
下载地址:http://mina.apache.org/downloads.html
2.下载SLF4J Android版
二、新建TestMina的android项目,命名空间为TestMina.com,Activity 名为TestMinaActivity,修改TestMinaActivity类源码如下:
package TestMina.com;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class TestMinaActivity extends Activity {
/** Called when the activity is first created. */
private static final int PORT = 9123;
private static final String TAG = "MyActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "bengin");
//System.setProperty("java.net.preferIPv6Addresses", "false");
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset
.forName("UTF-8"))));
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
Log.v(TAG, "begin bind");
try {
acceptor.bind(new InetSocketAddress(PORT));
Log.v(TAG, " bind end");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v(TAG, e.getMessage());
}
}
}
三、在eclipse 中新建TimeServerHandler类,添加如下源码
package TestMina.com;
import java.util.Date;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
public class TimeServerHandler extends IoHandlerAdapter {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
cause.printStackTrace();
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String str = message.toString();
System.out.println("Message read:");
System.out.println(str);
Date date = new Date();
session.write(date.toString());
System.out.println("Message written...");
session.close(true);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
System.out.println("IDLE " + session.getIdleCount(status));
}
}
四、
AndroidManifest.xml中加入"android.permission.INTERNET"权限,用于网络访问。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="TestMina.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestMinaActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
四、在真机环境下,打开android设备的wifi,在浏览器中输入android设备的ip访问。
http://192.168.100.100:9123/
系统返回:
Sun May 13 16:47:21 鏍兼灄灏兼不鏍囧噯鏃堕棿+0800 2012
或使用
telnet 192.168.100.100 9123,同样返回时间
测试结束
参考资料如下,但都没给出android完整实现,上面的代码是实现。
android使用mina需要注意的问题
http://www.cnblogs.com/sky7034/archive/2011/08/05/2128210.html