如何在更改活动之前检查Socket是否已建立

时间:2017-05-22 08:36:14

标签: android sockets

每当我运行我的代码时,在建立套接字连接时,我没有被重定向到下一个活动,而是移动到else部分并显示无法连接toast。我知道它从我的其他设备的输出正确连接。我的IF条件有问题吗?或者" Socket!= null"使用不当?

public void connecttoserver(View view){
    final TextView txtenterlumens;
    txtenterlumens = (TextView) findViewById(R.id.txtenterlumens);

    if(!txtenterlumens.getText().toString().equals("")) {
        enterlumens = Integer.parseInt(txtenterlumens.getText().toString());
        new Thread(new ClientThread()).start();
        if(socket!=null){
            Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(goToNextActivity);
        }
        else {
            Toast.makeText(LoginActivity.this, "Fail to connect!", Toast.LENGTH_LONG).show();
            txtenterlumens.setText("");
        }

    }
    else{
        Toast.makeText(LoginActivity.this, "Please enter correct Lumens!", Toast.LENGTH_LONG).show();
        txtenterlumens.setText("");
    }


}

这是我的套接字创建代码:

public class ClientThread implements Runnable {

    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
            setSocket(socket);

        } catch (UnknownHostException e1) {
            e1.printStackTrace();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您正在ClientThread创建套接字,但在另一个类中验证它,但您不会检索它。将此方法添加到ClientThread

public boolean isConnected(){
    return socket !=null
}

然后稍微修改您的connecttoserver

ClientThread ct = new new ClientThread();
new Thread(ct).start();
        if(ct.isConnected(){...}

答案 1 :(得分:0)

您在另一个线程上初始化套接字,因此无法立即获得结果。让我们做类似下面的事情。我还没有测试过这段代码。

public void connecttoserver(View view){
    final TextView txtenterlumens;
    txtenterlumens = (TextView) findViewById(R.id.txtenterlumens);

    if(!txtenterlumens.getText().toString().equals("")) {
        enterlumens = Integer.parseInt(txtenterlumens.getText().toString());
        new Thread(new ClientThread(new EstablishListener() {
            public void onEstablishCompleted(Socket socket) {
                setSocket(socket);
                if(socket!=null){
                    Intent goToNextActivity = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(goToNextActivity);
                }
                else {
                    Toast.makeText(LoginActivity.this, "Fail to connect!", Toast.LENGTH_LONG).show();
                    txtenterlumens.setText("");
                }

            }
        })).start();
    }
    else{
        Toast.makeText(LoginActivity.this, "Please enter correct Lumens!", Toast.LENGTH_LONG).show();
        txtenterlumens.setText("");
    }


}

public class ClientThread implements Runnable {
    EstablishListener mListener = null;
    public ClientThread(EstablishListener listner) {
        mListener = listner;
    }
    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
            mListener.onEstablishCompleted(socket);

        } catch (UnknownHostException e1) {
            mListener.onEstablishCompleted(null);
            e1.printStackTrace();

        } catch (IOException e1) {
            mListener.onEstablishCompleted(null);
            e1.printStackTrace();
        }
    }
}

// Create function interface for callback
public interface EstablishListener {
    void onEstablishCompleted(Socket socket);
}

答案 2 :(得分:0)

感谢大家的帮助。我决定,如果我在ClientThread中检查套接字连接和活动更改,那将是最简单的。

{{1}}