简体   繁体   中英

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. Basically the user enters their username in a 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(). I believe that the Strings are null at that point, so do I have to reinitialize these same three strings again in the 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.

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) .

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 {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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