变量“token”在代码中的某个点之后将其自身设置为null

时间:2016-11-14 14:52:29

标签: java android

我的一个变量出了问题。在代码中的某个点,它将自己设置为在某个特定点处为空。

public class RegisterAccountActivity extends Activity {

EditText usernameEditText, emailEditText, passwordEditText;
private String token;

private SharedPreferencesManager sharedPreferencesManager;
private AlertDialogManager alertDialogManager;

public void setToken(String token) {
    this.token = token;
}

public String getToken() {
    return this.token;
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_account);
    sharedPreferencesManager = new SharedPreferencesManager();
    alertDialogManager = new AlertDialogManager();

    usernameEditText = (EditText) findViewById(R.id.registerUserNameEditText);
    emailEditText = (EditText) findViewById(R.id.registerEmailEditText);
    passwordEditText = (EditText) findViewById(R.id.registerPasswordEditText);        
}

public void submitAccountBtnClicked(View view) throws JSONException
{
    String username = usernameEditText.getText().toString();
    String email = emailEditText.getText().toString();
    String password = passwordEditText.getText().toString();
    String storedToken;
    CheckBox checkbox = (CheckBox) findViewById(R.id.regCheckBox);

    if(username.trim().length() > 0 && email.trim().length() > 0 && password.trim().length() > 0)
    {
        JSONObject obj = new JSONObject();
        obj.put("name", username);
        obj.put("email", email);
        obj.put("password", password);
        String jsonDocument = obj.toString();
        postAccountTask pat = new postAccountTask();
        pat.execute("http://api.evang.dk/v2/users", jsonDocument);

此时此处,变量似乎松动并且变为空

        storedToken = getToken();

        if(checkbox.isChecked())
        {
            sharedPreferencesManager.saveData(RegisterAccountActivity.this, "USERNAME", username);
            sharedPreferencesManager.saveData(RegisterAccountActivity.this, "EMAIL", email);
            sharedPreferencesManager.saveData(RegisterAccountActivity.this, "PASSWORD", password);
            sharedPreferencesManager.saveData(RegisterAccountActivity.this, "TOKEN", storedToken);
        } else {
            sharedPreferencesManager.removeData(RegisterAccountActivity.this, "USERNAME");
            sharedPreferencesManager.removeData(RegisterAccountActivity.this, "EMAIL");
            sharedPreferencesManager.removeData(RegisterAccountActivity.this, "PASSWORD");
            sharedPreferencesManager.removeData(RegisterAccountActivity.this, "TOKEN");
        }

        Intent intent = new Intent(getBaseContext(), MainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("createToken", storedToken);
        bundle.putString("createUsername", username);
        intent.putExtra("createBundle", bundle);
        startActivity(intent);

    } else {
        alertDialogManager.showAlertDialog(this, "Registration failed", "Username, Email and Password can't be empty", false);
    }
}


private class postAccountTask extends AsyncTask<String, Void, CharSequence>
{
    public String tokenSession;

    @Override
    protected CharSequence doInBackground(String... params) {
        String urlString = params[0];
        String jsonDocument = params[1];
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
            osw.write(jsonDocument);
            osw.flush();
            osw.close();
            int responseCode = connection.getResponseCode();
            if(responseCode / 100 != 2){
                String responseMessage = connection.getResponseMessage();
                throw  new IOException("HTTP response code: " + responseCode + " " + responseMessage);
            }
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            try {
                StringBuffer stringBuffer = new StringBuffer();
                int character;
                while ((character = bufferedReader.read()) != -1)
                {
                    stringBuffer.append((char) character);
                    tokenSession = stringBuffer.toString();
                }
                int start = tokenSession.indexOf("{\"token\":\"") + "{\"token\":\"".length();
                int end = tokenSession.indexOf("\"}", start);
                String finalToken = tokenSession.substring(start, end);
                setToken(finalToken);
                return finalToken;
            }

            finally {
                bufferedReader.close();
            }

        } catch (ProtocolException e)
        {
            String msg = e.getMessage();
            return msg;
        } catch (MalformedURLException e) {
            String msg = e.getMessage();
            return msg;
        } catch (IOException e) {
            String msg = e.getMessage();
            return msg;
        }

    }
  }
}

0 个答案:

没有答案