0

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");
            }

1 Answers1

0

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

whatisthejava
  • 481
  • 3
  • 12