【问题标题】:Netty framework or how to use it with an AsyncTaskNetty 框架或如何将其与 AsyncTask 一起使用
【发布时间】:2012-06-13 07:52:23
【问题描述】:

我正在尝试开发一个将使用 Netty 的 Android 应用程序。

首先我想在Android上测试Netty,所以我要开发EchoClient例子。

我正在“翻译”客户端部分。这部分有两个类:EchoClientEchoClientHandler

EchoClient 作为线程运行,EchoClientHandler 处理所有网络内容。

在 main 方法中,EchoClient 是这样运行的:

new EchoClient(host, port, firstMessageSize).run();

EchoClientHandler 使用异步事件编程模型。

这是EchoClient的一段代码:

public void run() {
    // Configure the client.
    ClientBootstrap bootstrap = new ClientBootstrap(
            new NioClientSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    // Set up the pipeline factory.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new EchoClientHandler(firstMessageSize));
        }
    });

    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
}

这个run() 方法可以是AsyncTask.doBackground() 方法。

如您所见,EchoClientHandler 属于此类。

这是我想在 UI 线程中使用的 EchoClientHandler 方法:

@Override
public void messageReceived(
        ChannelHandlerContext ctx, MessageEvent e) {
    // Send back the received message to the remote peer.
    transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
    e.getChannel().write(e.getMessage());
}

如何在 AsynTask 中使用 EchoClientHandler?当messageReceived 被调用时,我不知道如何更新onProgressUpdate 上的TextView。

有什么建议吗?

【问题讨论】:

  • 你有没有让这个工作?我正在尝试将 Netty 集成到 Android 应用程序中并碰壁。
  • @Ash 不,我没有成功。

标签: android multithreading asynchronous android-asynctask netty


【解决方案1】:

也许你可以使用回调。

第 1 步。定义接口

public interface MyCallback {
     public void onMessage(string msg);
}

第二步在 EchoHandler 构造函数中取一个符合该接口的对象,并将其存储为类变量

private MyCallback _myCallback
public EchoClientHandler(int firstMessageSize, MyCallback callback) {
   _myCallback = myCallback;
   ...
}

第 3 步。 将对象传递给管道中的构造函数。 myCallback 可以来自 run() 作为参数,也可以来自 EchoClient 构造函数。

// Set up the pipeline factory.
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() throws Exception {
        return Channels.pipeline(
                new EchoClientHandler(firstMessageSize, myCallback));
    }
})

第 4 步。 在消息处理程序中调用回调

@Override
public void messageReceived(
    ChannelHandlerContext ctx, MessageEvent e) {
    // Send back the received message to the remote peer.
    transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes());
    e.getChannel().write(e.getMessage());

    _myCallback.onMessage("message");
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 2016-05-22
    • 2012-04-22
    • 2010-09-09
    相关资源
    最近更新 更多