开发者社区> 问答> 正文

android netty java.lang.ClassCastExcepti?400报错

android netty java.lang.ClassCastException: java.lang.String org.jboss.netty? 400 报错 @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)

throws Exception {

ChannelBuffer buf = (ChannelBuffer) e.getMessage();  

}

 (ChannelBuffer) e.getMessage()这句出错,我打印出来的  MessageEvent 的类类型结果是 string。

为什么呢。android 客户端的,用netty通信。求大神解答下小弟的问题。

展开
收起
爱吃鱼的程序员 2020-05-30 23:44:26 450 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    你得看pipeline中上一个handler抛出的是什么类型的? 你这里上面应该是将channelbuffer转为了string,然后抛出######

    引用来自“Tsingxu”的答案

    你得看pipeline中上一个handler抛出的是什么类型的? 你这里上面应该是将channelbuffer转为了string,然后抛出
    //--创建客户端channel的辅助类,发起connection请求
            bootstrap = new ClientBootstrap(
                    new NioClientSocketChannelFactory(
                            Executors.newCachedThreadPool(),
                            Executors.newCachedThreadPool()));


            // Set up the pipeline factory.
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
                public ChannelPipeline getPipeline()
                throws Exception {
                    ChannelPipeline pipeline = Channels.pipeline();
                    pipeline.addLast("decoder", new StringDecoder());
                    pipeline.addLast("encoder", new StringEncoder());
                    // and then business logic.
                    pipeline.addLast("handler", new ClientHandler());
                    return pipeline;
                }
            });
            
            bootstrap.setOption("tcpNoDelay", true);  
            bootstrap.setOption("keepAlive", true);
            
            //--创建无连接传输channel的辅助类(UDP)
            channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));
            channelFuture.awaitUninterruptibly();
            
            //--获取通信端口,用来发送数据。
            channel = channelFuture.awaitUninterruptibly().getChannel();
    ######头一回用netty,不懂######好了,弄好了,谢谢你。
    2020-05-30 23:44:28
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载