将字符串从TextView设置为TCPClient.class字符串IP时出现问题

时间:2017-07-14 15:35:49

标签: java android tcp

晚上好, 我正在尝试将文本数据从 TextView 从MainActivity传递到Client.class( TCP客户端)并将其设置为另一个字符串(实际上我正在传递IP集)在MainActivity中的TextView中,只是尝试在Client.class中加载它,但是当我试图用toast来形象化它时(如果我已经传递变量那么测试就有这样的东西)

enter image description here

此处客户端代码:

public class Client {

static Intent intent = getIntent();
static String getIp = intent.getExtra("key");
private String serverMessage;
public static final String SERVERIP = getIp; //your computer IP address
public static final int SERVERPORT = 4444;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;

MainActivity main;
PrintWriter out;
BufferedReader in;
/**
 *  Constructor of the class. OnMessagedReceived listens for the messages received from server
 */
public Client(OnMessageReceived listener) {
    mMessageListener = listener;
}

/**
 * Sends the message entered by client to the server
 * @param message text entered by client
 */
public void sendMessage(String message){
    if (out != null && !out.checkError()) {
        out.println(message);
        out.flush();
    }
}

public void stopClient(){
    mRun = false;
}

public void run() {

    mRun = true;

    try {
        //here you must put your computer's IP address.
        InetAddress serverAddr = InetAddress.getByName(SERVERIP);

        Log.e("TCP Client", "C: Connecting...");

        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, SERVERPORT);

        try {

            //send the message to the server
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

            Log.e("TCP Client", "C: Sent.");

            Log.e("TCP Client", "C: Done.");

            //receive the message which the server sends back
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            //in this while the client listens for the messages sent by the server
            while (mRun) {
                serverMessage = in.readLine();

                if (serverMessage != null && mMessageListener != null) {
                    //call the method messageReceived from MyActivity class
                    mMessageListener.messageReceived(serverMessage);
                }
                serverMessage = null;

            }

            Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");

        } catch (Exception e) {

            Log.e("TCP", "S: Error", e);

        } finally {
            //the socket must be closed. It is not possible to reconnect to this socket
            // after it is closed, which means a new socket instance has to be created.
            socket.close();
        }

    } catch (Exception e) {

        Log.e("TCP", "C: Error", e);

    }

}

//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
    void messageReceived(String message);
}

}

MainActivity:

        Intent i = new Intent(MainActivity.this, Client.class);
        i.putExtra("STRING_I_NEED", String.valueOf(indr));

1 个答案:

答案 0 :(得分:0)

在你MainActivity做这样的事情:

Intent i = new Intent(MainActivity.this, Client.class);
i.putextra("key", IPTextView);
// IPTextView is the IP address you want to toast

并在Client class执行以下操作:

String getIp = getIntent.getExtra("key")

你可以Toast赞成:

Toast.makeText(context, getIp, Toast.LENGTH_SHORT).show();

您拥有的其他选项是在SharedPreferences中保存IP地址并将其恢复到Client class

或者你可以创建一个带有一些返回值的静态方法,然后你可以通过类名获得Client class中的IP。

相关问题