0

我正在使用 Gottox 编写的用于 Java 的 Socket.IO-Client 制作一个 android 应用程序。我正在尝试使用该库将消息发送到由 node.js 制作的服务器并接收来自它的响应,但我收到了这些警告

09-10 20:36:34.910: W/System.err(395): a.a.a.SocketIOException: Error while handshaking
09-10 20:36:34.910: W/System.err(395):  at a.a.a.IOConnection.handshake(IOConnection.java:305)
09-10 20:36:34.910: W/System.err(395):  at a.a.a.IOConnection.access$7(IOConnection.java:275)
09-10 20:36:34.910: W/System.err(395):  at a.a.a.IOConnection$ConnectThread.run(IOConnection.java:191)
09-10 20:36:34.910: W/System.err(395): Caused by: java.net.SocketTimeoutException: Transport endpoint is not connected
09-10 20:36:34.921: W/System.err(395):  at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
09-10 20:36:34.921: W/System.err(395):  at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:246)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
09-10 20:36:34.931: W/System.err(395):  at java.net.Socket.connect(Socket.java:1055)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
09-10 20:36:34.931: W/System.err(395):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
09-10 20:36:34.931: W/System.err(395):  at a.a.a.IOConnection.handshake(IOConnection.java:296)

这是我的代码示例

 tv=(TextView)findViewById(R.id.textView1);
        SocketIO socket=new SocketIO();
        try {
            socket = new SocketIO("http://41.69.21.123:1333/");
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        socket.connect(new IOCallback() {
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                try {

                    tv.setText(json.toString(2));
                 //   System.out.println("Server said:" + json.toString(2));


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            public void onMessage(String data, IOAcknowledge ack) {
                //tv.setText(data);
                //System.out.println("Server said: " + data);
            }

            public void onError(SocketIOException socketIOException) {
                //tv.setText(socketIOException.toString());
                //System.out.println("an Error occured");

                socketIOException.printStackTrace();
            }

            public void onDisconnect() {
            //  tv.setText("Connection terminated.");
                //System.out.println("Connection terminated.");
            }

            public void onConnect() {
            //  tv.setText("Connection terminated.");
              //  System.out.println("Connection established");
            }

            public void on(String event, IOAcknowledge ack, Object... args) {
                //tv.setText("Server triggered event '" + event + "'");
               // System.out.println("Server triggered event '" + event + "'");
            }
        });

        // This line is cached until the connection is establisched.
        socket.send("Hello Server!");

   //     SocketIO socket=new SocketIO("http://127.0.0.1:3001/");

    }
4

1 回答 1

0

您正在初始化套接字两次。

这个:

...
SocketIO socket=new SocketIO();
try {
    socket = new SocketIO("http://41.69.21.123:1333/");
...

应该是这样的:

...
SocketIO socket;
try {
    socket = new SocketIO("http://41.69.21.123:1333/");
...
于 2013-02-23T08:08:38.447 回答