Not getting response in socket connection Not getting response in socket connection json json

Not getting response in socket connection


For socket driven events it is difficult to implement many functions while there are some (open source) libraries to achieve such a task. Consider using Socket.io.

Properties headers = new Properties();headers.setProperty("Content-Type","application/json"); // your headersSocketIO socketIO = SocketIO(url, headers);

For more information have a look at SocketIO docs

Edit

In your given example you should use HttpURLConnection as you are getting a response from server, you do not need to implement sockets. Simply GET or POST to fetch or push your data using HttpURLConnection.


For socket connection in android, you can use this gist file that simply implement socket connection.

public SocketConnection(OnStatusChanged statusChangedListener, OnMessageReceived messageReceivedListener) {        mStatusListener = statusChangedListener;        mMessageListener = messageReceivedListener;        isRunning = true;        try {            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);            mStatusListener.statusChanged(WAITING);            socket = new Socket(serverAddr, SERVER_PORT);            try {                printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);                mStatusListener.statusChanged(CONNECTED);                bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));                while (isRunning) {                    retrieveMessage = bufferedReader.readLine();                    if (retrieveMessage != null && mMessageListener != null) {                        mMessageListener.messageReceived(retrieveMessage);                    }                    else {                        mStatusListener.statusChanged(DISCONNECTED);                    }                    retrieveMessage = null;                }            } catch (Exception e) {                mStatusListener.statusChanged(ERROR);            } finally {                socket.close();            }        } catch (Exception e) {            mStatusListener.statusChanged(ERROR);        }    }

UPDATE

If you want to set custom header, just should write them to printWriter .

When you write

writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n"); 

The \r\n\r\n bit is sending a line-feed/carriage-return to end the line and then another one to indicate that there are no more headers. This is a standard in both HTTP and email formats, i.e. a blank line indicates the end of headers. In order to add additional headers you just need to not send that sequence until you're done. You can do the following instead

writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); writer.print ("header1: value1\r\n"); writer.print ("header2: value2\r\n"); writer.print ("header3: value3\r\n"); // end the header sectionwriter.print ("\r\n"); 


The code is probably stuck in readLine() because the server still waits for the request's completion.

You could change it to :

// queryout.write("POST /setMap HTTP/1.1\r\n");// headersout.write("Host: 192.168.1.1\r\n");out.write("Content-Type: application/json; charset=utf-8\r\n");out.write("Content-Length: " + json.toString().getBytes(StandardCharsets.UTF_8).length + "\r\n");// end of the headersout.write("\r\n");// bodyout.write(json.toString());// actually send the requestout.flush();Log.i("Webservice", "json.toString"+json.toString());

I think that you are using the wrong tool to make HTTP requests. Sockets are low level network channels, you have to do a lot of things yourself.

You should consider using an HttpURLConnection instead. If possible I strongly suggest to take a even higher level approach, and use something like retrofit2 for example.