title: How to read JSON data instead of a line from socket?
body: <p>My application connects to a sensor over WiFi direct. Then it connects to a socket and is supposed to read data. The data is JSON and looks like {"d":{temp_mC";33416,"humidity_ppm"...more data....}}. The sensor sends data every 2 minutes. Before I only have been able to read lines that are sent by a server but since the sensor does not send lines I need to redo my TCPClient class. I think that I could implement this code.</p>
  
  <pre><code>ByteArrayOutputStream result = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int length;
  while ((length = socket.getInputStream().read(buffer)) != -1) {
  result.write(buffer, 0, length);
  }
  // StandardCharsets.UTF_8.name() &gt; JDK 7
  return result.toString("UTF-8");
  </code></pre>
  
  <p>To get the JSON data into a string, then I would have to parse it but that is for later.</p>
  
  <p>My old code looks like this, does anyone have an idea how I could remake my old code to implement above method or a similar method so I can read the JSON data and turn it into a string so I can parse it later?</p>
  
  <p>TcpClient</p>
  
  <pre><code>public class TcpClient {
  
  ....
  
  /**
   * Constructor of the class. OnMessagedReceived listens for the messages received from server
   */
  public TcpClient(OnMessageReceived listener) {
      mMessageListener = listener;
  
  }
  
  /**
   * Sends the message entered by client to the server
   *
   * @param message text entered by client
   */
  
  /**
   * Close the connection and release the members
   */
  public void stopClient() {
  
      mRun = false;
  
      mMessageListener = null;
      mBufferIn = null;
      mServerMessage = null;
  }
  
  public void run() {
  
      mRun = true;
  
      try {
          //here you must put your computer's IP address.
          InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
  
          Log.e("TCP Client", "C: Connecting...");
  
          //create a socket to make the connection with the server
          Socket socket = new Socket(serverAddr, SERVER_PORT);
  
          try {
  
  
              //receives the message which the server sends back
              mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
  
              //in this while the client listens for the messages sent by the server
              while (mRun) {
  
                  mServerMessage = mBufferIn.readLine();
  
                  if (mServerMessage != null &amp;&amp; mMessageListener != null) {
                      //call the method messageReceived from MyActivity class
                      mMessageListener.messageReceived(mServerMessage);
                  }
  
              }
  
              Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
  
          } catch (Exception e) {
  
              Log.e("TCP", "S: Error", e);
  
          } finally {
              //the socket must be closed. It is not possible to reconnect to this socket
              // after it is closed, which means a new socket instance has to be created.
              socket.close();
          }
  
      } catch (Exception e) {
  
          Log.e("TCP", "C: Error", e);
  
      }
  
  }
  
  //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
  //class at on asynckTask doInBackground
  public interface OnMessageReceived {
      public void messageReceived(String message);
  }
  
  }
  </code></pre>
  
  <p>My AsyncClass in other activity</p>
  
  <pre><code>  public class ConnectTask extends AsyncTask&lt;String, String, TcpClient&gt; {
  
      @Override
      protected TcpClient doInBackground(String... message) {
  
          //we create a TCPClient object
          mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
              @Override
              //here the messageReceived method is implemented
              public void messageReceived(String message) {
                  //this method calls the onProgressUpdate
                  publishProgress(message);
              }
          });
          mTcpClient.run();
  
          return null;
      }
  
      @Override
      protected void onProgressUpdate(String... values) {
          super.onProgressUpdate(values);
          CO2 = values[0];
          //response received from server
          Log.d("CO2", values[0]);
          //process server response here....
  
      }
  
  }
  </code></pre>