課程
/移動開發
/Android
/長連接利器—網絡框架解析之mina篇
線程中沒有實例化ConnectionManager對象吧?通過構造者模式返回的config沒有被使用
2017-02-15
源自:長連接利器—網絡框架解析之mina篇 5-1
正在回答
對應著我的寫,就可以了。
private ConnectionConfig mConfig;private WeakReference<Context> mContext;private NioSocketConnector mConnection;private IoSession mSession;private InetSocketAddress mAddress;public ?ConnectionManager(ConnectionConfig config){ ? ?this.mConfig = config; ? ?this.mContext = new WeakReference<>(config.getContext()); ? ?init();}private void init() { ? ?mAddress = new InetSocketAddress(mConfig.getIp(),mConfig.getPort()); ? ?mConnection = new NioSocketConnector(); ? ?mConnection.getSessionConfig().setReadBufferSize(mConfig.getReadBufferSize()); ? ?mConnection.getFilterChain().addLast("logger",new LoggingFilter()); ? ?mConnection.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); ? ?mConnection.setHandler(new DefaultHandler(mContext.get())); ? ?mConnection.setDefaultRemoteAddress(mAddress);}/** * 外層調用取得與服務器的連接 * @return */public boolean connect(){ ? ?Log.e("tag", "準備連接"); ? ?try{ ? ? ? ?ConnectFuture future = mConnection.connect(); ? ? ? ?future.awaitUninterruptibly(); ? ? ? ?mSession = future.getSession(); ? ? ? ?SessionManager.getInstance().setSeesion(mSession); ? ?}catch (Exception e){ ? ? ? ?e.printStackTrace(); ? ? ? ?Log.e("tag", "連接失敗"); ? ? ? ?return false; ? ?} ? ?return mSession == null ? false : true;}/** * 斷開連接的方法 */public void disConnection(){ ? ?mConnection.dispose();//Dispose后,對象都不存在了 ? ?mConnection = null; ? ?mSession ? ?= null; ? ?mAddress ? ?= null; ? ?mContext ? ?= null;}
舉報
apache mina框架如何在android中使用,并能通過mina與服務器進行通信
3 回答mina如何去實現重連
1 回答老師您好,我們要實現實時的扣費操作,用這個框架合適嗎?
2 回答能提供下課程代碼嗎?
1 回答我想分享慕課的課程
1 回答Mina適合在Android中用嗎 ? 會不會很耗電 ?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-02-16
對應著我的寫,就可以了。
2017-02-16
private ConnectionConfig mConfig;
private WeakReference<Context> mContext;
private NioSocketConnector mConnection;
private IoSession mSession;
private InetSocketAddress mAddress;
public ?ConnectionManager(ConnectionConfig config){
? ?this.mConfig = config;
? ?this.mContext = new WeakReference<>(config.getContext());
? ?init();
}
private void init() {
? ?mAddress = new InetSocketAddress(mConfig.getIp(),mConfig.getPort());
? ?mConnection = new NioSocketConnector();
? ?mConnection.getSessionConfig().setReadBufferSize(mConfig.getReadBufferSize());
? ?mConnection.getFilterChain().addLast("logger",new LoggingFilter());
? ?mConnection.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
? ?mConnection.setHandler(new DefaultHandler(mContext.get()));
? ?mConnection.setDefaultRemoteAddress(mAddress);
}
/**
* 外層調用取得與服務器的連接
* @return
*/
public boolean connect(){
? ?Log.e("tag", "準備連接");
? ?try{
? ? ? ?ConnectFuture future = mConnection.connect();
? ? ? ?future.awaitUninterruptibly();
? ? ? ?mSession = future.getSession();
? ? ? ?SessionManager.getInstance().setSeesion(mSession);
? ?}catch (Exception e){
? ? ? ?e.printStackTrace();
? ? ? ?Log.e("tag", "連接失敗");
? ? ? ?return false;
? ?}
? ?return mSession == null ? false : true;
}
/**
* 斷開連接的方法
*/
public void disConnection(){
? ?mConnection.dispose();//Dispose后,對象都不存在了
? ?mConnection = null;
? ?mSession ? ?= null;
? ?mAddress ? ?= null;
? ?mContext ? ?= null;
}