简体   繁体   English

假人的字符串初始化

[英]String Initialization for Dummies

OK I am messing something simple up here. 好吧,我在这里弄乱了一些简单的东西。

I have three Classes all within an Activity: 我在一个活动中有三个班级:

public class ActivityUserAccountCreate extends Activity implements
        OnClickListener {

}

private class postToHttps extends AsyncTask<String, Integer, String> {
@Override
    protected String doInBackground(String... params) {

        try {
            createUserAccount();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

public void createUserAccount() throws ClientProtocolException, IOException {
...
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

Log.d("Posting Username", usernameFinal);
nameValuePairs.add(new BasicNameValuePair("username", "usernameFinal"));
...

}

In my Activity class I initialize three Strings which are in the layout. 在我的Activity类中,我初始化了布局中的三个String。 Basically the user enters their username in a EditText. 基本上,用户在EditText中输入其用户名。 The strings are = to get the text form the field. 字符串是=以获取字段中的文本。 That all works fine, but I am trying to use those same Strings in the createUserAccount(). 一切正常,但我试图在createUserAccount()中使用这些相同的字符串。 I believe that the Strings are null at that point, so do I have to reinitialize these same three strings again in the createUserAccount()? 我认为此时Strings为null,因此是否必须在createUserAccount()中再次重新初始化这三个字符串? If so, should I do it the same way that I did in in the Activity Class? 如果是这样,我是否应该像在活动类中那样做?

Thanks 谢谢

OK I have edited according to the recommendation below, to: 好的,我已经根据以下建议进行了编辑,以:

...
new postToHttps().execute(usernameFinal,passwordFinal,userEmail);
...

private class postToHttps extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
        String usernameFinal = params[0];
            String passwordFinal = params[1];
            String userEmail = params[2];

            try {
                createUserAccount(usernameFinal, passwordFinal, userEmail);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

public void createUserAccount(String usernameFinal, String passwordFinal, String userEmail) throws ClientProtocolException, IOException {

    String uri = "editedout.php";
    Log.d("Action", "Posting user data to php");
    HttpClient client = new DefaultHttpClient();
    HttpPost getMethod = new HttpPost(uri);
    Log.d("Posting Location", uri);
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    Log.d("Posting Username", usernameFinal);
    nameValuePairs
            .add(new BasicNameValuePair("username", usernameFinal));

    Log.d("Posting Pass", passwordFinal);
    nameValuePairs
            .add(new BasicNameValuePair("password", passwordFinal));

    nameValuePairs.add(new BasicNameValuePair("email", userEmail));

    getMethod
            .setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
    client.execute(getMethod);
    Log.d("Action", "Finished Posting Data to PHP");
}

but am getting a crash. 但是崩溃了 it looks like it is crashing in the doInBackground, but am still learning to read the LogCat. 看起来好像在doInBackground中崩溃了,但仍在学习读取LogCat。

You shouldn't have to be reinitializing anything, but if you are trying to pass in three Strings into your AsyncTask , I suggest taking advantage of the variable arguments used in doInBackground(String... params) . 您不必重新初始化任何东西,但是如果您尝试将三个String传递到AsyncTask ,建议使用doInBackground(String... params)使用的变量参数。

For example: 例如:

...
private class PostToHttps extends AsyncTask<String, Integer, String> {
@Override
    protected String doInBackground(String... params) {
        String username = params[0];
        String firstName = params[1];
        String lastName = params[2];

        createUserAccount(username, firstName, lastName);
    }
...

PostToHttps httpsTask = new PostToHttps();
httpsTask.execute(usernameEditText.getText().toString(),
                  firstNameEditText.getText().toString(),
                  lastNameEditText.getText().toString);

...

public void createUserAccount(String username, String firstName, String lastName)
    throws ClientProtocolException, IOException {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM