title: Java socket Json
body: <p><strong>UPDATED</strong></p>
  
  <p>There is my updated code after the response,
  I have one problem, now, i didn't receive anything :</p>
  
  <p><strong>Problem</strong> : </p>
  
  <pre><code>Connexion cliente reçue.
  Lancement du traitement de la connexion cliente
  Exception in thread "Thread-1" java.lang.NullPointerException
      at cuisine.TimeServer.onResponseReceived(TimeServer.java:85)
      at cuisine.ClientProcessor.run(ClientProcessor.java:43)
      at java.base/java.lang.Thread.run(Thread.java:844)
  </code></pre>
  
  <p>TimeServer: 85 : Callback </p>
  
  <pre><code>  @Override
    public void onResponseReceived(String response) {
      // When the response is received from ClientProcessor
      // this method is called (by ClientProcessor).
      // Your response is the parameter String response.
      callback.onResponse(response);
    }
  </code></pre>
  
  <p>Client processor: 43 :</p>
  
  <pre><code>server.onResponseReceived(response);
  </code></pre>
  
  <p>One client can connect but i can't receive anything : Json or text.</p>
  
  <p>There is my new Java Code : </p>
  
  <p><strong>Main.java :</strong></p>
  
  <pre><code>   import cuisine.TimeServer.OnResponseReceivedListener;
  
  
  public class main implements OnResponseReceivedListener {
  
     public static void main(String[] args) throws IOException {
  
          String host = "192.168.1.21";
          int port = 8080;
  
          TimeServer ts = new TimeServer(host, port);
          ts.open();
          System.out.println("------------Connected ! ------------");
  
     }
  
     @Override
     public void onResponse(String response) {
       doSomethingWith(response);
       System.out.println("REPONSE : ");
       System.out.println(response);
  
     }
  
     private void doSomethingWith(String response) {
           System.out.println("REPONSE : ");
           System.out.println(response);
     }
  
  
  }
  </code></pre>
  
  <p>I don't really know how to use the response now, because the sysout "RESPONSE" is not in my console
  <strong>TimeServer.java :</strong> </p>
  
  <pre><code>...
  import cuisine.ClientProcessor.Server;
  
  
  public class TimeServer implements Server {
  
    public interface OnResponseReceivedListener {
      void onResponse(String response);
    }
  
    private int port = 2345;
    private String host = "127.0.0.1";
    private ServerSocket server = null;
    private boolean isRunning = true;
    private OnResponseReceivedListener callback;
  
    public TimeServer() {
      try {
        server = new ServerSocket(port, 100, InetAddress.getByName(host));
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  
    public TimeServer(String pHost, int pPort) {
      host = pHost;
      port = pPort;
      try {
        server = new ServerSocket(port, 100, InetAddress.getByName(host));
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  
    public void open() {
  
      Thread t =
          new Thread(
              new Runnable() {
                public void run() {
                  while (isRunning == true) {
  
                    try {
                      Socket client = server.accept();
  
                      System.out.println("Connexion cliente reçue.");
                      ClientProcessor c = new ClientProcessor(client);
                      Thread t = new Thread(c);
                      // Don't forget to define the Server for ClientProcessor
                      c.addServer(TimeServer.this);
                      t.start();
  
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                  }
  
                  try {
                    server.close();
                  } catch (IOException e) {
                    e.printStackTrace();
                    server = null;
                  }
                }
              });
  
      t.start();
    }
  
    @Override
    public void onResponseReceived(String response) {
      // When the response is received from ClientProcessor
      // this method is called (by ClientProcessor).
      // Your response is the parameter String response.
      callback.onResponse(response);
    }
  
    public void addOnResponseReceivedListener(OnResponseReceivedListener listener) {
      callback = listener;
    }
  
    public void removeOnResponseReceivedListener() {
      callback = null;
    }
  
    public void close() {
      isRunning = false;
    }
  }
  </code></pre>
  
  <p><strong>ClientProcessor.java :</strong> </p>
  
  <pre><code>public class ClientProcessor implements Runnable {
  
        public interface Server {
          void onResponseReceived(String response);
        }
  
        private Socket sock;
        private PrintWriter writer = null;
        private BufferedInputStream reader = null;
        public List&lt;Dish&gt; dish;
        // Your server instance
        private Server server;
  
        public ClientProcessor(Socket pSock) {
          this.sock = pSock;
        }
  
        public void run() {
          System.err.println("Lancement du traitement de la connexion cliente");
  
          boolean closeConnexion = false;
          while (!sock.isClosed()) {
  
            try {
  
              writer = new PrintWriter(sock.getOutputStream());
              reader = new BufferedInputStream(sock.getInputStream());
  
              String response = read();
  
              // report the response to TimeServer
              server.onResponseReceived(response);
  
              InetSocketAddress remote = (InetSocketAddress) sock.getRemoteSocketAddress();
  
              String debug = "";
              debug = "Thread : " + Thread.currentThread().getName() + ". ";
              debug += "Demande de l'adresse : " + remote.getAddress().getHostAddress() + ".";
              debug += " Sur le port : " + remote.getPort() + ".\n";
              debug += "\t -&gt; Commande reçue : " + response + "\n";
              System.err.println("\n" + debug);
  
              String toSend = "";
  
              switch (response.toUpperCase()) {
                case "ORDER":
                  toSend = "Dish";
                  break;
                case "CLOSE":
                  toSend = "Communication terminée";
                  closeConnexion = true;
                  break;
                default:
                  toSend = "Commande inconnu !";
                  break;
              }
  
              writer.write(toSend);
              writer.flush();
  
              if (closeConnexion) {
                System.err.println("COMMANDE CLOSE DETECTEE ! ");
                writer = null;
                reader = null;
                sock.close();
                break;
              }
            } catch (SocketException e) {
              System.err.println("LA CONNEXION A ETE INTERROMPUE ! ");
              break;
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
  
        public void addServer(Server server) {
          this.server = server;
        }
  
        private void removeServer() {
          server = null;
        }
  
        // La méthode que nous utilisons pour lire les réponses
        private String read() throws IOException {
          String response = "";
          int stream;
          byte[] b = new byte[4096];
          stream = reader.read(b);
          response = new String(b, 0, stream);
          return response;
        }
      }
  </code></pre>
  
  <p>Thanks,
  Benjamin.</p>