課程
/移動開發(fā)
/Android
/長連接利器—網(wǎng)絡(luò)框架解析之mina篇
線程中沒有實(shí)例化ConnectionManager對象吧?通過構(gòu)造者模式返回的config沒有被使用
2017-02-15
源自:長連接利器—網(wǎng)絡(luò)框架解析之mina篇 5-1
正在回答
對應(yīng)著我的寫,就可以了。
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);}/** * 外層調(diào)用取得與服務(wù)器的連接 * @return */public boolean connect(){ ? ?Log.e("tag", "準(zhǔn)備連接"); ? ?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;}
舉報(bào)
apache mina框架如何在android中使用,并能通過mina與服務(wù)器進(jìn)行通信
3 回答mina如何去實(shí)現(xiàn)重連
1 回答老師您好,我們要實(shí)現(xiàn)實(shí)時的扣費(fèi)操作,用這個框架合適嗎?
2 回答能提供下課程代碼嗎?
1 回答我想分享慕課的課程
1 回答Mina適合在Android中用嗎 ? 會不會很耗電 ?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-02-16
對應(yīng)著我的寫,就可以了。
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);
}
/**
* 外層調(diào)用取得與服務(wù)器的連接
* @return
*/
public boolean connect(){
? ?Log.e("tag", "準(zhǔn)備連接");
? ?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;
}