netty - Sending and receiving JSON -
i using apache httpcomponets post data netty. use gson working json.
request.post("http://localhost:9090/print").bodystring(getjson(), contenttype.application_json),
this netty code not seem receive json response. sure code wrong. error here ?
@override public void channelread(channelhandlercontext ctx, object msg) throws exception { logger.info( "channelread" + msg ); if( msg instanceof httprequest ){ this.request = ( httprequest ) msg; logger.info( "[" + request.geturi() + "]"); } if( msg instanceof httpcontent ){ httpcontent content = (httpcontent)msg; bytebuf buf = content.content(); logger.info( "[" + buf.tostring(charsetutil.utf_8) + "]"); if (msg instanceof lasthttpcontent) { lasthttpcontent trailer = (lasthttpcontent) msg; writeresponse( trailer, ctx); } } } private boolean writeresponse(httpobject currentobj, channelhandlercontext ctx) { logger.info( getjson() ); bytebuf response = unpooled.copiedbuffer( getjson(), charsetutil.utf_8); ctx.write( response ); /* 'iskeepalive' method in api ? */ //boolean keepalive = iskeepalive(request); //keepalive; return false; }
you need call ctx.writeandflush(...) actual flush out socket.
Comments
Post a Comment