Android中的回调和自定义套接字客户端

时间:2016-09-13 11:25:11

标签: android sockets

我使用了本教程https://causeyourestuck.io/2016/04/27/node-js-android-tcpip/中的代码,但遇到了这样的错误:

  

无法解析方法' send(java.lang.String)'

     

无法解决方法' disconnect()'

Client.java类中的问题,我是从教程(上面的链接)创建的,但我无法解决这个问题。

  

MainActivity.java

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();
}
  

Client.java

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);
}

}

1 个答案:

答案 0 :(得分:1)

那是因为您在Socket上调用了这些方法(来自您的回调),而不是Client(在另一个范围内),因为他们拥有相同的名称和java将使用最小的变量范围。

更改回调变量的名称或Client变量的名称,在使用正确的变量时它应该有效:)

例如:

void doIt() {
    final Client myClient = new Client("192.168.0.8", 1234);
    myClient.setClientCallback(new Client.ClientCallback () {
        @Override
        public void onMessage(String message) {
        }

        @Override
        public void onConnect(Socket socket) {
            myClient.send("Hello World!\n");
            myClient.disconnect();
        }

        @Override
        public void onDisconnect(Socket socket, String message) {
        }

        @Override
        public void onConnectError(Socket socket, String message) {
        }
    });

    myClient.connect();
}