0

我目前正在创建一个项目,该项目需要一个简单的异步任务来处理在幕后运行的线程。用户需要登录。我正在使用另一个名为 PVAndroid Client 的类,它提供有用的方法并为我提供了一个 XML 序列化程序表单数据包。我对使用线程或对服务器做任何事情都是全新的,所以这可能是完全错误的或有些正确的。

我得到了用户输入的数据:IP 地址和端口、他们的用户名(我将其分为名字和姓氏)、他们选择的区域。我加密了他们的密码,并尝试使用 IP 地址和端口号连接到 tcp。我正在尝试执行异步任务,但对我应该做什么感到困惑。任何人都可以指导我正确的方向并帮助我吗?

谢谢你,我真的很感激。

    private TcpClient myTcpClient = null;
    private UdpClient udpClient;
    private static final String USERNAME_SHARED_PREFS = "username";
    private static final String PASSWORD_SHARED_PREFS = "password";
    private static final String IP_ADDRESS_SHARED_PREFS = "ipAddressPref";
    private static final String PORT_SHARED_PREFS = "portNumberPref";
    private String encryptedNameLoginActivity, encryptPassLoginActivity;
    private EditText userText, passText;
    private String getIpAddressSharedPrefs, getPortNumberPrefs;
    private String getUserNameValue;
    private String getPasswordValue;
    private String fName, lName;
    private SharedPreferences settings;
    private Editor myEditor;
    private boolean getCheckedRemember;
    private boolean resultCheck = false;
    private int portNum;
    private Button submitButton;
    private String userMACVARIABLE = "";
    private String regionSelected, gridSelected;
    private Spinner regSpinner, gridSpinner;
    PVDCAndroidClient client;
    private int userNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        client = new PVDCAndroidClient();
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Take care of getting user's login information:
        submitButton = (Button) findViewById(R.id.submitButton);
        userText = (EditText) findViewById(R.id.nameTextBox);
        passText = (EditText) findViewById(R.id.passwordTextBox);
        regSpinner = (Spinner) findViewById(R.id.regionSpinner);

        // grid selected as well? sometime?
        regSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long rowId) {
                regionSelected = regSpinner.getItemAtPosition(position)
                        .toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
        submitButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                settings = PreferenceManager
                        .getDefaultSharedPreferences(AndroidClientCompnt.this);

                getIpAddressSharedPrefs = settings.getString(
                        IP_ADDRESS_SHARED_PREFS, "");
                portNum = Integer.parseInt(settings.getString(
                        PORT_SHARED_PREFS, ""));

                if (getIpAddressSharedPrefs.length() != 0 && portNum != 0) {
                    if (userText.length() != 0 && passText.length() != 0) {
                        try {

                            try {
                                // encrypting the user's password.
                                encryptPassLoginActivity = Secure.encrypt(passText
                                        .toString());
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            // first connect attempt.
                            myTcpClient = new TcpClient();
                            myTcpClient.connect(getIpAddressSharedPrefs,
                                    portNum);
                            // here is where I want to call Async to do login
                            // or do whatever else.

                            UploadTask task = new UploadTask();
                            task.execute();


                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(),
                                    "Could not connect.", Toast.LENGTH_LONG)
                                    .show();
                            e.printStackTrace();
                        }
                    }

                }
            }
        });
    }

    private class UploadTask extends AsyncTask<String, Integer, Void> 
    {
        @Override
        protected void onPreExecute() {
            Toast.makeText(getApplicationContext(), "Loading...",
                    Toast.LENGTH_LONG).show();
                            }

        @Override
        protected Void doInBackground(String... names) {

                    resultCheck = myTcpClient.connect(getIpAddressSharedPrefs,
                            portNum);


                    if (resultCheck == true) {
                        while (myTcpClient.getUserNum() < 0) {
                            // num set? session? with proxy server?
                        }

                        String[] firstAndLast;

                        String spcDelmt = " ";

                        firstAndLast = userText.toString().split(spcDelmt);

                        fName = firstAndLast[0];
                        lName = firstAndLast[1];

                        // set up the tcp client to sent the information to the
                        // server.
                        client.login(fName, lName, encryptPassLoginActivity,regionSelected, 128, 128, 20);

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Connection not successful", Toast.LENGTH_LONG)
                                .show();
                    }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Toast.makeText(getApplicationContext(), "Connected",
                    Toast.LENGTH_LONG).show();
        }
    }

}
4

2 回答 2

1

第一的

@Override
protected Void doInBackground(String...params) {
    new Thread (new Runnable() {
       // ...
    }
}

永远不要再这样做了。无需在实际在后台运行的doInBackground方法中创建新Thread的。所以删除它。 Thread

给你的建议很棘手,因为你需要阅读Threads、使用Connection等。所以对你最好的建议是阅读一些tutorials基本应用程序的示例和阅读参考资料。所以你可以从这里开始:

Android TCP Client and Server Communication Programming–Illustrated with Example

于 2012-07-09T19:13:57.070 回答
0

我看不到你在哪里执行你的任务,但我看到你在做一些奇怪的事情doInBackground()!绝对没有理由在其中创建自己的线程。删除它,你可以像这样使用你的任务:

UploadTask task = new UploadTask();
task.execute("someString", "anotherString", "addAsManyStringsYouNeed");

AsyncTask的文档也很有帮助。

于 2012-07-09T19:17:47.197 回答