title: Callbacks and custom socket client in Android
body: <p>I used code from this tutorial <a href="https://causeyourestuck.io/2016/04/27/node-js-android-tcpip/" rel="nofollow">https://causeyourestuck.io/2016/04/27/node-js-android-tcpip/</a>, but got the such errors:</p>
  
  <blockquote>
    <p>Cannot resolve method 'send(java.lang.String)' </p>
    
    <p>Cannot resolve method 'disconnect()'</p>
  </blockquote>
  
  <p>Problem in Client.java class, which I created from tutorial (link above), but I can't resolve this problem.</p>
  
  <blockquote>
    <p><strong>MainActivity.java</strong></p>
  </blockquote>
  
  <pre><code>void doIt() {
      Client socket = new Client("192.168.0.8", 1234);
      socket.setClientCallback(new Client.ClientCallback () {
          @Override
          public void onMessage(String message) {
          }
  
          @Override
          public void onConnect(Socket socket) {
              socket.send("Hello World!\n");
              socket.disconnect();
          }
  
          @Override
          public void onDisconnect(Socket socket, String message) {
          }
  
          @Override
          public void onConnectError(Socket socket, String message) {
          }
      });
  
      socket.connect();
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Client.java</strong></p>
  </blockquote>
  
  <pre><code>public class Client {
  private Socket socket;
  private OutputStream socketOutput;
  private BufferedReader socketInput;
  
  private String ip;
  private int port;
  private ClientCallback listener=null;
  
  public Client(String ip, int port){
      this.ip=ip;
      this.port=port;
  }
  
  public void connect(){
      new Thread(new Runnable() {
          @Override
          public void run() {
              socket = new Socket();
              InetSocketAddress socketAddress = new InetSocketAddress(ip, port);
              try {
                  socket.connect(socketAddress);
                  socketOutput = socket.getOutputStream();
                  socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
                  new ReceiveThread().start();
  
                  if(listener!=null)
                      listener.onConnect(socket);
              } catch (IOException e) {
                  if(listener!=null)
                      listener.onConnectError(socket, e.getMessage());
              }
          }
      }).start();
  }
  
  public void disconnect(){
      try {
          socket.close();
      } catch (IOException e) {
          if(listener!=null)
              listener.onDisconnect(socket, e.getMessage());
      }
  }
  
  public void send(String message){
      try {
          socketOutput.write(message.getBytes());
      } catch (IOException e) {
          if(listener!=null)
              listener.onDisconnect(socket, e.getMessage());
      }
  }
  
  private class ReceiveThread extends Thread implements Runnable{
      public void run(){
          String message;
          try {
              while((message = socketInput.readLine()) != null) {   // each line must end with a \n to be received
                  if(listener!=null)
                      listener.onMessage(message);
              }
          } catch (IOException e) {
              if(listener!=null)
                  listener.onDisconnect(socket, e.getMessage());
          }
      }
  }
  
  public void setClientCallback(ClientCallback listener){
      this.listener=listener;
  }
  
  public void removeClientCallback(){
      this.listener=null;
  }
  
  public interface ClientCallback {
      void onMessage(String message);
      void onConnect(Socket socket);
      void onDisconnect(Socket socket, String message);
      void onConnectError(Socket socket, String message);
  }
  </code></pre>
  
  <p>}</p>