netty4 能不能运行在android上

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了netty4 能不能运行在android上相关的知识,希望对你有一定的参考价值。

是哪些
在修改androidpn,感觉xmpp协议太重了,而且据说很耗流量很耗电。
mina2两年没更新了!spring mvc 竟然还用的是2.5
= - =想迁移到netty4,把xmpp改成轻量的protobuf,但是不知道android上能否流畅使用netty4
netty4的jar包,服务端不要紧可以使用all-in-one,android端怎么使用?最小依赖是哪些
坐等大神解惑,感激不尽。
参考技术A netty-buffer-4.0.17.Final.jar

netty-codec-4.0.17.Final.jar

netty-common-4.0.17.Final.jar

netty-handler-4.0.17.Final.jar

netty-transport-4.0.17.Final.jar

bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup());
bootstrap.channelFactory(new ChannelFactory<NiosocketChannel>()
@Override
public NioSocketChannel newChannel()
NioSocketChannel channel=new NioSocketChannel();
Log.d("ClientConnection", PooledByteBufAllocator.DEFAULT.toString());
channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
return channel;

);

解决传输文件的时候爆没有sun buf类的问题
channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);本回答被提问者和网友采纳
参考技术B 相比Netty3, Netty4有很多显著的变化:
NioEventLoopGroup 是一个处理I/O操作的多线程事件环。即为Netty4里的线程池,在3.x里,一个Channel是由ChannelFactory创建的,同时新创建的Channel会自动注册到一个隐藏的I/O线程。 4.0使用新的名为EventLoopGroup的接口来替换ChannelFactory,它由一个或多个EventLoop来构成。一个新的 Channel不会自动注册到EventLoopGroup,但用户可以显式调用EventLoopGroup.register()来注册。在Server端的Bootstrap参数中,有两个EventLoopGroup,第一个通常称为'boss',用于接收发来的连接请求。第二个称为'worker',,用于处理boss接受并且注册给worker的连接中的信息。

ChannelInitializer是一个特殊的handler,用于方便的配置用户自定义的handler实现,如代码中所示。在channelRegistered的生命周期中会触发用户复写的initChannel(C ch)方法,并且在调用后会讲自身从channelPipeline中移除。
代码示例
?
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;

/**
* DateTime: 2015年1月5日 上午9:56:10
*
*/
public class HelloWorldServer
static final boolean SSL = System.getProperty("ssl") != null;
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));

public static void main(String[] args) throws Exception
// Configure SSL.
final SslContext sslCtx;
if (SSL)
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
else
sslCtx = null;


// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>()
@Override
public void initChannel(SocketChannel ch) throws Exception
ChannelPipeline p = ch.pipeline();
if (sslCtx != null)
p.addLast(sslCtx.newHandler(ch.alloc()));

p.addLast(new LoggingHandler(LogLevel.INFO));
p.addLast(
new ObjectEncoder(),
new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
new HelloWorldServerHandler());

);

// Start the server.
ChannelFuture f = b.bind(PORT).sync();

// Wait until the server socket is closed.
f.channel().closeFuture().sync();
finally
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();




class HelloWorldServerHandler extends ChannelInboundHandlerAdapter

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
ctx.write("server write msg:"+msg);


@Override
public void channelReadComplete(ChannelHandlerContext ctx)
ctx.flush();


@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

/**
* DateTime: 2015年1月5日 上午9:56:22
*
*/
public class HelloWorldClient
static final boolean SSL = System.getProperty("ssl") != null;
static final String HOST = System.getProperty("host", "127.0.0.1");
static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));

public static void main(String[] args) throws Exception
// Configure SSL.git
final SslContext sslCtx;
if (SSL)
sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);

else
sslCtx = null;


// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>()
@Override
public void initChannel(SocketChannel ch) throws Exception
ChannelPipeline p = ch.pipeline();
if (sslCtx != null)
p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));

p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
new HelloWorldClientHandler());

);

// Start the client.
ChannelFuture f = b.connect(HOST, PORT).sync();

// Wait until the connection is closed.
f.channel().closeFuture().sync();

finally
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();




class HelloWorldClientHandler extends ChannelInboundHandlerAdapter

private final String msg = "hello java world";

/**
* Creates a client-side handler.
*/
public HelloWorldClientHandler()
//TODO


@Override
public void channelActive(ChannelHandlerContext ctx)
ctx.writeAndFlush(msg);


@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
System.out.println(msg);
// ctx.write(msg);


@Override
public void channelReadComplete(ChannelHandlerContext ctx)
ctx.flush();


@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();

在 Android 上添加 spdy 编解码器后使用 netty 连接失败

【中文标题】在 Android 上添加 spdy 编解码器后使用 netty 连接失败【英文标题】:Failed to connect using netty after add spdy codec on Android 【发布时间】:2013-09-27 05:24:49 【问题描述】:

我已经使用 netty 编写了一个服务器和一个本地代理。该项目在我的电脑上运行良好,但是当我稍作更改以使其成为 android 应用程序时,它就无法运行。

我试了很多次,发现插入下面的代码后就出问题了。

pipeline.addLast("spdyEncoder", new SpdyFrameEncoder(3));
pipeline.addLast("spdyDecoder", new SpdyFrameDecoder(3));

我比较了添加或不添加spdy编解码器的日志,区别是

找不到类 'com.jcraft.jzlib.Deflater',引用自方法 io.netty.handler.codec.spdy.SpdyHeaderBlockJZlibEncoder.”,所以我猜问题出在jzlib deflater,但我真的不知道如何修复它。谁能帮帮我,非常感谢。

这里是 LogCat:

09-22 03:35:15.761: D/Multiplex(863): initInstance
09-22 03:35:15.761: D/MultiplexChannelConnMgr(863): constructor
09-22 03:35:15.771: I/dalvikvm(863): Could not find method org.slf4j.LoggerFactory.getILoggerFactory, referenced from method io.netty.util.internal.logging.Slf4JLoggerFactory.<init>
09-22 03:35:15.771: W/dalvikvm(863): VFY: unable to resolve static method 28005: Lorg/slf4j/LoggerFactory;.getILoggerFactory ()Lorg/slf4j/ILoggerFactory;
09-22 03:35:15.771: D/dalvikvm(863): VFY: replacing opcode 0x71 at 0x0026
09-22 03:35:15.781: I/dalvikvm(863): Could not find method org.slf4j.LoggerFactory.getLogger, referenced from method io.netty.util.internal.logging.Slf4JLoggerFactory.newInstance
09-22 03:35:15.781: W/dalvikvm(863): VFY: unable to resolve static method 28006: Lorg/slf4j/LoggerFactory;.getLogger (Ljava/lang/String;)Lorg/slf4j/Logger;
09-22 03:35:15.794: D/dalvikvm(863): VFY: replacing opcode 0x71 at 0x0002
09-22 03:35:15.862: W/System.err(863): log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).
09-22 03:35:15.871: W/System.err(863): log4j:WARN Please initialize the log4j system properly.
09-22 03:35:15.871: W/System.err(863): log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
09-22 03:35:16.110: D/dalvikvm(863): GC_CONCURRENT freed 121K, 9% free 2660K/2904K, paused 70ms+38ms, total 213ms
09-22 03:35:16.180: E/dalvikvm(863): Could not find class 'javassist.ClassPool', referenced from method io.netty.util.internal.JavassistTypeParameterMatcherGenerator.<clinit>
09-22 03:35:16.180: W/dalvikvm(863): VFY: unable to resolve new-instance 2027 (Ljavassist/ClassPool;) in Lio/netty/util/internal/JavassistTypeParameterMatcherGenerator;
09-22 03:35:16.180: D/dalvikvm(863): VFY: replacing opcode 0x22 at 0x0008
09-22 03:35:16.180: I/dalvikvm(863): Could not find method javassist.ClassPool.getAndRename, referenced from method io.netty.util.internal.JavassistTypeParameterMatcherGenerator.generate
09-22 03:35:16.190: W/dalvikvm(863): VFY: unable to resolve virtual method 16552: Ljavassist/ClassPool;.getAndRename (Ljava/lang/String;Ljava/lang/String;)Ljavassist/CtClass;
09-22 03:35:16.190: D/dalvikvm(863): VFY: replacing opcode 0x6e at 0x0032
09-22 03:35:16.190: I/dalvikvm(863): Could not find method javassist.ClassPool.appendClassPath, referenced from method io.netty.util.internal.JavassistTypeParameterMatcherGenerator.appendClassPath
09-22 03:35:16.190: W/dalvikvm(863): VFY: unable to resolve virtual method 16550: Ljavassist/ClassPool;.appendClassPath (Ljava/lang/String;)Ljavassist/ClassPath;
09-22 03:35:16.190: D/dalvikvm(863): VFY: replacing opcode 0x6e at 0x0002
09-22 03:35:16.190: W/dalvikvm(863): VFY: unable to find class referenced in signature (Ljavassist/ClassPath;)
09-22 03:35:16.190: I/dalvikvm(863): Could not find method javassist.ClassPool.appendClassPath, referenced from method io.netty.util.internal.JavassistTypeParameterMatcherGenerator.appendClassPath
09-22 03:35:16.190: W/dalvikvm(863): VFY: unable to resolve virtual method 16551: Ljavassist/ClassPool;.appendClassPath (Ljavassist/ClassPath;)Ljavassist/ClassPath;
09-22 03:35:16.190: D/dalvikvm(863): VFY: replacing opcode 0x6e at 0x0002
09-22 03:35:16.190: D/dalvikvm(863): DexOpt: unable to opt direct call 0x40a5 at 0x0b in Lio/netty/util/internal/JavassistTypeParameterMatcherGenerator;.<clinit>
09-22 03:35:16.200: D/dalvikvm(863): DexOpt: unable to opt direct call 0x40a4 at 0x16 in Lio/netty/util/internal/JavassistTypeParameterMatcherGenerator;.<clinit>
09-22 03:35:16.200: W/dalvikvm(863): Exception Ljava/lang/NoClassDefFoundError; thrown while initializing Lio/netty/util/internal/JavassistTypeParameterMatcherGenerator;
09-22 03:35:16.260: D/MultiplexChannelConnMgr(863): Before connect to 10.108.112.239:9001
09-22 03:35:16.330: E/dalvikvm(863): Could not find class 'com.jcraft.jzlib.Deflater', referenced from method io.netty.handler.codec.spdy.SpdyHeaderBlockJZlibEncoder.<init>
09-22 03:35:16.330: W/dalvikvm(863): VFY: unable to resolve new-instance 574 (Lcom/jcraft/jzlib/Deflater;) in Lio/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder;
09-22 03:35:16.330: D/dalvikvm(863): VFY: replacing opcode 0x22 at 0x0005
09-22 03:35:16.340: W/dalvikvm(863): VFY: unable to resolve instance field 1051
09-22 03:35:16.340: D/dalvikvm(863): VFY: replacing opcode 0x54 at 0x0003
09-22 03:35:16.340: W/dalvikvm(863): VFY: unable to resolve instance field 1051
09-22 03:35:16.340: D/dalvikvm(863): VFY: replacing opcode 0x5b at 0x0049
09-22 03:35:16.350: W/dalvikvm(863): VFY: unable to resolve instance field 1051
09-22 03:35:16.350: D/dalvikvm(863): VFY: replacing opcode 0x5b at 0x000b
09-22 03:35:16.361: I/dalvikvm(863): Could not find method com.jcraft.jzlib.Deflater.deflateEnd, referenced from method io.netty.handler.codec.spdy.SpdyHeaderBlockJZlibEncoder.end
09-22 03:35:16.361: W/dalvikvm(863): VFY: unable to resolve virtual method 4173: Lcom/jcraft/jzlib/Deflater;.deflateEnd ()I
09-22 03:35:16.361: D/dalvikvm(863): VFY: replacing opcode 0x6e at 0x000b
09-22 03:35:16.361: D/dalvikvm(863): DexOpt: unable to opt direct call 0x104b at 0x07 in Lio/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder;.<init>
09-22 03:35:16.381: D/MultiplexChannelConnMgr(863): Catch error: null
**09-22 03:35:16.381: W/System.err(863): java.nio.channels.ClosedChannelException**

【问题讨论】:

【参考方案1】:

您需要将 com.jcraft.jzlib 添加到您的类路径中。这是通过添加 jzlib jar 来完成的。

http://www.jcraft.com/jzlib/

【讨论】:

非常感谢。我很高兴能解决这个问题。事实上问题同样很明显,但没有你的帮助我仍然无法继续前进。我确实需要学习如何解决问题。再次感谢。

以上是关于netty4 能不能运行在android上的主要内容,如果未能解决你的问题,请参考以下文章

如何在真实设备上运行 Android 应用程序?

在 android for windows 上运行 kivy

是否可以在不做任何更改的情况下在 Android 手机中运行 JavaFX 桌面应用程序? [复制]

java.lang.NoSuchMethodError public default void android.content.ServiceConnection.onBindingDied(andr

React-Native Expo,Facebook 登录无法在 Android 设备上运行

hornor8改user模式为debug模式