title: Android App Connecting to Node.js server using Socket.io
body: <p>I'm having trouble getting my Android app to connect to a socket.io chat server. I'm using socket.io-java-client created by Gottox which can be found here: <a href="https://github.com/Gottox/socket.io-java-client" rel="noreferrer">https://github.com/Gottox/socket.io-java-client</a></p>
  
  <p>The server runs locally over port 7000. I'm using the android emulator, so I'm using 10.0.2.2:7000 to access the server.</p>
  
  <p>Any help would be appreciated, I don't have much experience at all with SSL. If I find a working solution I'll also post it.</p>
  
  <p><strong>Node.js Server</strong></p>
  
  <pre><code>var express = require('express');
  var app = express();
  var server = require('http').createServer(app).listen(7000);
  var io = require('socket.io').listen(server);
  io.sockets.on('connection', function(client){
      client.on('message', function(err, msg){
          client.broadcast.emit('message', msg);
      });
   });
  </code></pre>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>{
    "name": "simplechat",
    "version": "0.0.1",
    "main": "app.js",
    "dependencies": {
      "express" : "~4.0.0",
      "socket.io" : "~0.9.13"
    }
  }
  </code></pre>
  
  <p><strong>Android: SendMessageActivity</strong></p>
  
  <pre><code>public class SendMessageActivity extends Activity {
  
      private static final String SERVER_ADDRESS = "https://10.0.2.2:7000";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_send_message);
  
          System.out.println("Sever: " + SERVER_ADDRESS);
  
          try {
              SocketIO socket = new SocketIO(new URL(SERVER_ADDRESS), new IOCallback() {
                  @Override
                  public void onDisconnect() {
                      System.out.println("disconnected");
                  }
  
                  @Override
                  public void onConnect() {
                      System.out.println("connected");
                  }
  
                  @Override
                  public void onMessage(String s, IOAcknowledge ioAcknowledge) {
                  }
  
                  @Override
                  public void onMessage(JSONObject jsonObject, IOAcknowledge ioAcknowledge) {
                  }
  
                  @Override
                  public void on(String event, IOAcknowledge ioAcknowledge, Object... objects) {
                  }
  
                  @Override
                  public void onError(SocketIOException e) {
                      e.printStackTrace();
                  }
              });
  
          } catch (MalformedURLException ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p><strong>Android Permissions</strong></p>
  
  <pre><code>&lt;uses-permission
      android:name="android.permission.INTERNET"&gt;
  &lt;/uses-permission&gt;
  </code></pre>
  
  <p><strong>Error Code</strong></p>
  
  <pre><code>08-09 16:07:28.224    8411-8441/com.example.puma.chatexample W/System.err﹕ io.socket.SocketIOException: Error while handshaking
  08-09 16:07:28.225    8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:322)
  08-09 16:07:28.225    8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.access$600(IOConnection.java:39)
  08-09 16:07:28.225    8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
  08-09 16:07:28.226    8411-8441/com.example.puma.chatexample W/System.err﹕ Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'javax.net.ssl.SSLSocketFactory javax.net.ssl.SSLContext.getSocketFactory()' on a null object reference
  08-09 16:07:28.226    8411-8441/com.example.puma.chatexample W/System.err﹕ at io.socket.IOConnection.handshake(IOConnection.java:302)
  08-09 16:07:28.227    8411-8441/com.example.puma.chatexample W/System.err﹕ ... 2 more
  </code></pre>