BufferedReader.readline()的结果在Android中返回任何结果 [英] BufferedReader.readline() return nothing in result in android

查看:170
本文介绍了BufferedReader.readline()的结果在Android中返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作套接字编程在Android和试图从我的Andr​​oid应用程序读取服务器的反响。

有关这个我使用bufferreader.readline(),但它没有任何returen事情

我在机器人工作室Evalute前pression测试,它会在返回结果只是结果=

下面是我的code,我已经使用
 尝试
                {
                    如果(插座!= NULL)
                    {

  //Log.i(\"AsynkTask,readFromStream:读消息);
                    //消息= dis.toString();                   / * InputStreamReader的isrdr =新的InputStreamReader(socket.getInputStream());
                    如果(isrdr.ready())
                    {                    }
                    其他
                    {
                        消息=读者未准备好;
                    } * /
                    在的BufferedReader =新的BufferedReader(新的InputStreamReader(socket.getInputStream()));                        而(真)
                        {
                            //弦乐味精= in.readLine();
                            如果(in.readLine()== NULL){
                                消息=没有数据读取
                            }其他{
                                消息=数据可用;
                            }
                        }
                }
                其他
                {
                    消息=没有插座进行连接;
                    Log.i(AsynkTask,readFromStream:无法读取,关闭套接字);
                }
            }
            赶上(例外五)
            {
                消息=的异常出现
                Log.i(AsynkTask,readFromStream:写入失败);
            }


解决方案

ID检查您socket.getInputStream()没有返回空

此外,您的,而绝不会逃跑,直到抛出一个异常,以便ID更改,要成为一段时间(in.readLine()!= NULL)

修改

我会做到这一点。

 如果(socket.getInputStream()== NULL){
    //处理这个
}
其他{
     而(in.readLine()!= NULL){
            processString();
     }
}

}

我不知道为什么你的插槽为空,但我可以看不到它的任何初始化

i am working on socket programming in android and trying to read server responce from my android application .

For this i am using bufferreader.readline() but it is not returen any thing

I have tested it in android studio "Evalute Expression" , it will return just "result=" in result.

Here is my code which i have used try { if (socket != null) {

                    //Log.i("AsynkTask", "readFromStream : Reading message");
                    //message = dis.toString();

                   /*InputStreamReader isrdr=new InputStreamReader(socket.getInputStream());
                    if(isrdr.ready())
                    {

                    }
                    else
                    {
                        message="Reader Not Ready : ";
                    }*/
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                        while (true)
                        {
                            //String msg = in.readLine();
                            if (in.readLine() == null) {
                                message = "No data read";
                            } else {
                                message = "data available";
                            }
                        }
                }
                else
                {
                    message="No Socket Connected";
                    Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
                }
            }
            catch (Exception e)
            {
                message="Exception Occured";
                Log.i("AsynkTask", "readFromStream : Writing failed");
            }

解决方案

Id check that your socket.getInputStream() is not returning null

Also your while will never escape until an exception is thrown so id change that to be while (in.readLine() != null)

Edit

I would do this

if (socket.getInputStream() == null) {
    //deal with this 
}
else {
     while(in.readLine() != null) {
            processString();
     }
}

}

I have no idea why your socket is null but i can t see any initialisation for it

这篇关于BufferedReader.readline()的结果在Android中返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆