Back to project page SenqmAndroid.
The source code is released under:
GNU General Public License
If you think the Android project SenqmAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2014 Alban GRUIN/*from w ww. j av a 2 s . c om*/ * * This file is part of SenqmAndroid. * * SenqmAndroid is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * SenqmAndroid is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SenqmAndroid. If not, see <http://www.gnu.org/licenses/>. */ package com.agrn.senqm.network.connection; import com.agrn.senqm.network.Message; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONStringer; import java.nio.charset.Charset; import java.util.Iterator; public class ConnectionHandler extends ChannelInboundHandlerAdapter { private final Connection connection; public ConnectionHandler(Connection connection) { this.connection = connection; } @Override public void channelRead(ChannelHandlerContext context, Object messageReceived) { ByteBuf buffer = (ByteBuf) messageReceived; String bufferContent = buffer.toString(Charset.defaultCharset()); connection.messageReceived(jsonToMessage(bufferContent)); } @Override public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { context.close(); } private Message jsonToMessage(String json) { try { JSONObject jsonObject = new JSONObject(json); Message message = new Message(jsonObject.getString("content")); Iterator keys = jsonObject.keys(); while(keys.hasNext()) { String key = keys.next().toString(); if(!key.equalsIgnoreCase("content")) message.addValue(key.toLowerCase(), jsonObject.get(key)); } return message; } catch(JSONException e) { return null; } } }