0

我已经TCP使用套接字为 android 制作了一个客户端。但是,我拥有的程序不允许我动态输入服务器地址。我也不能使用意图来传输字符串,MainActivity因为我TcpClient.java没有扩展到Activity. 我应该实现什么逻辑,以便我可以动态设置服务器地址并连接到我希望的任何服务器..

MainActivity.java

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {

            case R.id.menu_settings:
                Intent i = new Intent(this, Preferences.class);
                startActivityForResult(i, RESULT_SETTINGS);
                break;

            }

            return true;
        }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            switch (requestCode) {
            case RESULT_SETTINGS:
               saveData();
                break;

            }

        }

        public class connectTask extends AsyncTask<String,String,TcpClient> {

            @Override
            protected TcpClient doInBackground(String... message) {

                //we create a TCPClient object and
                mTcpClient = new TcpClient(new TcpClient.OnMessageReceived() {
                    @Override
                    //here the messageReceived method is implemented
                    public void messageReceived(String message) {
                        //this method calls the onProgressUpdate
                        publishProgress(message);
                    }
                });
                mTcpClient.run(serverip, serverport);

               return null;
            }

        }


        //call it at Activity startup onStart() for example
        public void loadData(){

               int mode = Activity.MODE_PRIVATE;
               SharedPreferences mySharedPreferences;
               mySharedPreferences = getSharedPreferences(MYPREFS, mode);
               serverip = mySharedPreferences.getString("IP", serverip);
               serverport = mySharedPreferences.getInt("Port", serverport);
           }

           // Call it whenever you modify the values
           public void saveData()
           {
               int mode = Activity.MODE_PRIVATE;
               SharedPreferences mySharedPreferences;
               mySharedPreferences = getSharedPreferences(MYPREFS, mode);
               SharedPreferences.Editor editor = mySharedPreferences.edit();
               editor.putString("IP", serverip);
               editor.putInt("Port", serverport);
               editor.commit();
            Toast.makeText(getBaseContext(), 
                    "Server Settings Saved" + serverip + serverport , 
                     Toast.LENGTH_LONG).show();
           }

}

这不是保存数据。它在 Toast 上显示 null0。此外,这些是为上述代码声明的变量

public static final String MYPREFS = "192.168.1.3";
public String serverip;
public int serverport;

首选项.java

package com.example.homauto;

导入android.os.Bundle;导入android.preference.PreferenceActivity;

公共类首选项扩展 PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
} 

}

我是编程新手.. 这是我参考的网站.. Android TCP 连接教程

4

1 回答 1

0

好的,您可以做几件事来将 IP 和端口传递给 TCPClient 类。对我来说,最简单的方法是声明 run 方法如下:

public void run(String srvIP, int srvPort)
{
    mRun = true;
    try {
        //here you must put your computer's IP address.
        InetAddress serverAddr = InetAddress.getByName(srvIP);
        Log.e("TCP Client", "C: Connecting...");
        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, srvPort);

你必须这样称呼它:

 mTcpClient.run("ServerIP", ServerPort);
 like this: mTcpClient.run("192.168.1.25", 4444);

其他可能性是更改创建方法并将这些参数放在那里,

// You have to remove the final in these variables
public static String SERVERIP = "192.168.0.102"; //your computer IP address
public static int SERVERPORT = 4444;
 public TCPClient(String srvIP, int srvPort, OnMessageReceived listener) {
        SERVERIP=srvIP;
        SERVERPORT=srvPort;
        mMessageListener = listener;
    }

你像这样实例化这个类:

mTcpClient = new TcpClient(ServerIP, ServerPort, new TcpClient.OnMessageReceived()

现在,在您的应用程序(主要活动)中,您需要放置一个对话框或其他活动,以便在启动 TCPClient 类(在您的情况下为 AsyncTask)之前询问用户要连接的 IP 和端口。

我会放置一个操作栏菜单,单击时会显示一个对话框来询问这些值。

此外,您可以保存这些值,以便将来使用它们(在 MainActivity 中):

// call it at Activity startup onStart() for example
public void loadData()
{
    int mode = Activity.MODE_PRIVATE;
    SharedPreferences mySharedPreferences;
    mySharedPreferences = getSharedPreferences(Typedefs.saveConfigsFileName, mode);
    serverIP = mySharedPreferences.getString("IP", serverIP);
    serverPort = mySharedPreferences.getInt("Port", serverPort);
}

// Call it whenever you modify the values
public void saveData()
{
    int mode = Activity.MODE_PRIVATE;
    SharedPreferences mySharedPreferences;
    mySharedPreferences = getSharedPreferences(Typedefs.saveConfigsFileName, mode);
    SharedPreferences.Editor editor = mySharedPreferences.edit();
    editor.putString("IP", serverIP);
    editor.putInt("Port", serverPort);
    editor.commit();
}
于 2013-07-16T12:34:09.897 回答