在创建连接时面对Android websocket客户端中的错误

时间:2014-04-05 05:08:44

标签: android websocket

下面的代码尝试连接到LAN上的Websocket服务器。我试图从Android设备访问网页,它工作正常。但套接字连接不起作用

public class ClientConnection extends AsyncTask<URL, Void, Void> {

    protected static final String TAG = "change";

    public static  void  initWebSocketClient() {
        Log.d(TAG,"init client websckt request");
        try {
            URI url = new URI("ws://ipadress:8080/WebSocketDemo1/serverEndpointDemo1");
            WebSocket websocket = new WebSocketConnection(url);

            // Register Event Handlers
            websocket.setEventHandler(new WebSocketEventHandler() {
                    public void onOpen()
                    {
                        Log.d(TAG,"OPEN");
                    }

                    public void onMessage(WebSocketMessage message)
                    {
                            Log.d(TAG,"Message");
                    }

                    public void onClose()
                    {
                            Log.d(TAG,"Close");
                    }
            });

            // Establish WebSocket Connection
            websocket.connect();

            // Send UTF-8 Text
            websocket.send("hello world");



            // Close WebSocket Connection
            websocket.close();
        }
        catch (WebSocketException wse) {
                wse.printStackTrace();
        }
        catch (URISyntaxException use) {
                use.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(URL... params) {
        // TODO Auto-generated method stub
        initWebSocketClient();
        return null;
    }

}

我正在跟随调用来获取websocket连接..

try {
                //ClientConnection.getClientSession().getBasicRemote().sendObject(m);
                new ClientConnection().execute();
            }
            catch(Exception e) {
                e.printStackTrace();
            }

我收到的错误消息是

04-05 10:36:01.310: W/System.err(9252): de.roderick.weberknecht.WebSocketException: error while creating socket to ws://ipadress:8080/WebSocketDemo1/serverEndpointDemo1
04-05 10:36:01.310: W/System.err(9252):     at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:248)
04-05 10:36:01.310: W/System.err(9252):     at de.roderick.weberknecht.WebSocketConnection.connect(WebSocketConnection.java:87)
04-05 10:36:01.310: W/System.err(9252):     at com.example.conn.ClientConnection.initWebSocketClient(ClientConnection.java:66)
04-05 10:36:01.310: W/System.err(9252):     at com.example.conn.ClientConnection.doInBackground(ClientConnection.java:87)
04-05 10:36:01.310: W/System.err(9252):     at com.example.conn.ClientConnection.doInBackground(ClientConnection.java:1)
04-05 10:36:01.310: W/System.err(9252):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-05 10:36:01.310: W/System.err(9252):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-05 10:36:01.310: W/System.err(9252):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-05 10:36:01.310: W/System.err(9252):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-05 10:36:01.310: W/System.err(9252):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-05 10:36:01.310: W/System.err(9252):     at java.lang.Thread.run(Thread.java:1019)
04-05 10:36:01.310: W/System.err(9252): Caused by: java.net.SocketTimeoutException: Connection timed out
04-05 10:36:01.320: W/System.err(9252):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
04-05 10:36:01.320: W/System.err(9252):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
04-05 10:36:01.320: W/System.err(9252):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
04-05 10:36:01.320: W/System.err(9252):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
04-05 10:36:01.320: W/System.err(9252):     at java.net.Socket.startupSocket(Socket.java:724)
04-05 10:36:01.320: W/System.err(9252):     at java.net.Socket.tryAllAddresses(Socket.java:150)
04-05 10:36:01.320: W/System.err(9252):     at java.net.Socket.<init>(Socket.java:209)
04-05 10:36:01.320: W/System.err(9252):     at java.net.Socket.<init>(Socket.java:176)
04-05 10:36:01.320: W/System.err(9252):     at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:242)
04-05 10:36:01.330: W/System.err(9252):     ... 10 more

请提出建议,以便进行此代码编写

2 个答案:

答案 0 :(得分:1)

您可以在安装时检查局域网的端口,或者尝试使用此代码。

private void connectWebSocket() {
  URI uri;
  try {
    uri = new URI("ws://websockethost:8080");
  } catch (URISyntaxException e) {
    e.printStackTrace();
    return;
  }
  mWebSocketClient = new WebSocketClient(uri) {
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
      Log.i("Websocket", "Opened");
      mWebSocketClient.send("Hello from " + Build.MANUFACTURER + " " + Build.MODEL);
    }
    @Override
    public void onMessage(String s) {
      final String message = s;
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          TextView textView = (TextView)findViewById(R.id.messages);
          textView.setText(textView.getText() + "\n" + message);
        }
      });
    }
    @Override
    public void onClose(int i, String s, boolean b) {
      Log.i("Websocket", "Closed " + s);
    }
    @Override
    public void onError(Exception e) {
      Log.i("Websocket", "Error " + e.getMessage());
    }
  };
  mWebSocketClient.connect();
}

答案 1 :(得分:0)

而不是mconnection.sendtext只需将其更改为mbconnection.sendtext并尝试一次,或者在Web套接字服务器中进行检查