1

I'm getting mad trying to read data from a socket. I tried all and all, I think that my code should work but didn't.

My intention is to only run the following method on the onCreate. First I create a Thread to run all the things that are related to the network. Then, I create the scoket object and I read the inputstream of the socket. In this moment, in an infinit loop, I read the inputstream using readLine as explained saw on this answer. Finally, I do what I want with the data that came by the socket.

I don't know how much data will be sended by the server. And it would be in json, but this doesn't matter.

Here my code

public void receiveMsgs(){      
    new Thread(new Runnable(){
        @Override
        public void run() {
            BufferedReader in = null;
            try {
                Log.d("NETWORK-RECEIVE", "Trying to connect to socket...");
                Socket socket;
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                if(socket.isConnected()){
                    Log.d("NETWORK-RECEIVE", "The connection have been stablished");
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("NETWORK-RECEIVE", "Something goes wrong: IOException");
            }
            while(true){
                String msg = null;
                try {
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = in.readLine()) != null) {
                        total.append(line);
                    }                   
                    msg = total.toString();
                    Log.d("NETWORK-RECEIVE","Message readed!:"+msg);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("NETWORK-RECEIVE", "Something goes wrong: IOException");
                }
                if(msg == null){
                    Log.d("NETWORK-RECEIVE", "Message is null");
                    break;
                }
                else{
                    //Do what I want
                    Log.d("NETWORK-RECEIVE", "something");
                }
            }           
        }
    }).start();
}
8
  • are you trying it on emulator or device..? If you want data from server why aren't you using httpclient ? Commented Dec 6, 2013 at 20:27
  • Both, because we work with our own protocol using streams, and I have to post things via streams Commented Dec 6, 2013 at 20:29
  • Well, what happens what you run it? What log messages do you generate? Do you have any basis to believe the connection is even being established with the server? Commented Dec 6, 2013 at 20:33
  • 1
    Don't guess - add some step by step logging of successful results to your code too. And tell us what the actual observed behavior is. How do you know it's not working? Commented Dec 6, 2013 at 21:12
  • 1
    So what doesn't work? You really are not being very proactive, either about investigating the problem or about reporting the information which would be needed for anyone to help you. Commented Dec 6, 2013 at 22:42

1 Answer 1

1

Ever look at square's OKHttp or Retrofit?

http://square.github.io/okhttp/#examples

&

http://square.github.io/retrofit/

Seems easier than sockets. Do you really need sockets?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.