gpt4 book ai didi

java - 当我调用 bufferedReader.readLine() 时,我的 Android 应用程序卡住了;

转载 作者:行者123 更新时间:2023-11-30 01:56:30 29 4
gpt4 key购买 nike

我为 Android 手机和 Android Wear 创建了一个新的 Android Studio 项目来测试 ChannelApi .在我的项目中似乎一切正常,除了在 onChannelOpened 方法中我无法在不卡住应用程序的情况下从 InputStream 中读取。

这就是我从可穿戴设备发送消息的方式(这似乎有效):

Wearable.ChannelApi.openChannel(googleApiClient, node.getId(), channelIdentifier).setResultCallback(new ResultCallback<ChannelApi.OpenChannelResult>() {
@Override
public void onResult(ChannelApi.OpenChannelResult openChannelResult) {
final Channel channel = openChannelResult.getChannel();
channel.getOutputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetOutputStreamResult>() {
@Override
public void onResult(final Channel.GetOutputStreamResult getOutputStreamResult) {
final String message = "hello from your smartwatch";
try {
getOutputStreamResult.getOutputStream().write(message.getBytes());
Log.d(TAG, "sendMessageToNode: onResult: onResult: Message sent: " + message);
}
// ...

日志打印:D/MainActivity:sendMessageToNode: onResult: onResult: Message sent: hello from your smartwatch

在智能手机端,我尝试按如下方式阅读消息:

@Override
public void onChannelOpened(final Channel channel) {
channel.getInputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetInputStreamResult>() {
@Override
public void onResult(Channel.GetInputStreamResult getInputStreamResult) {
Log.d(TAG, "onChannelOpened: onResult");

String message = "";
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = getInputStreamResult.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
//while ((line = bufferedReader.readLine()) != null) {
while (true) {
// Next line is freezing the app
line = bufferedReader.readLine();
Log.d(TAG, "onChannelOpened: onResult: line: " + line);
message += line + "\n";
if (line == null) break;
}
Log.d(TAG, "onChannelOpened: onResult: Received the following message: \" + message");
// ...

日志打印:D/MainActivity:onChannelOpened: onResult 但仅此而已。

我启动了调试器,现在知道错误一定在这一行:line = bufferedReader.readLine();。但是没有抛出异常或其他任何东西。调试器甚至告诉我缓冲区有正确的值:

Debugger shows right values

我正在尝试发送“来自您的智能 watch 的问候”。但是,该应用程序似乎卡住了。有谁知道为什么吗?

最佳答案

您需要在发送方关闭流,否则接收方将不知道是否有更多数据通过 channel 传输。

关于java - 当我调用 bufferedReader.readLine() 时,我的 Android 应用程序卡住了;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127316/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com