Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 变量";代币;在代码中的某个点之后将自身设置为null_Java_Android - Fatal编程技术网

Java 变量";代币;在代码中的某个点之后将自身设置为null

Java 变量";代币;在代码中的某个点之后将自身设置为null,java,android,Java,Android,我的一个变量有问题。在代码中的某个点,它会在某个特定点将其自身设置为null public class RegisterAccountActivity extends Activity { EditText usernameEditText, emailEditText, passwordEditText; private String token; private SharedPreferencesManager sharedPreferencesManager; private Aler

我的一个变量有问题。在代码中的某个点,它会在某个特定点将其自身设置为null

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);
此时,变量似乎自行松动,变为null

        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;
        }

    }
  }
}
storedToken=getToken();
如果(checkbox.isChecked())
{
SharedReferencesManager.saveData(RegisterAccountActivity.this,“USERNAME”,USERNAME);
SharedReferencesManager.saveData(RegisterAccountActivity.this,“EMAIL”,EMAIL);
SharedReferencesManager.saveData(RegisterAccountActivity.this,“PASSWORD”,PASSWORD);
SharedReferencesManager.saveData(RegisterAccountActivity.this,“TOKEN”,storedToken);
}否则{
SharedReferencesManager.removeData(RegisterAccountActivity.this,“用户名”);
SharedReferencesManager.removeData(RegisterAccountActivity.this,“电子邮件”);
SharedReferencesManager.removeData(RegisterAccountActivity.this,“密码”);
SharedReferencesManager.removeData(RegisterAccountActivity.this,“令牌”);
}
Intent Intent=new Intent(getBaseContext(),MainActivity.class);
Bundle=新Bundle();
bundle.putString(“createToken”,storedToken);
bundle.putString(“createUsername”,username);
intent.putExtra(“createBundle”,bundle);
星触觉(意向);
}否则{
alertDialogManager.showAlertDialog(此“注册失败”,“用户名、电子邮件和密码不能为空”,false);
}
}
私有类postAccountTask扩展了AsyncTask
{
公共字符串令牌会话;
@凌驾
受保护的字符序列doInBackground(字符串…参数){
字符串urlString=params[0];
字符串jsonDocument=params[1];
试一试{
URL=新URL(URL字符串);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
connection.setDoOutput(真);
setRequestProperty(“内容类型”、“应用程序/json”);
setRequestProperty(“接受”、“应用程序/json”);
OutputStreamWriter osw=新的OutputStreamWriter(connection.getOutputStream());
写作(jsonDocument);
osw.flush();
osw.close();
int responseCode=connection.getResponseCode();
如果(响应代码/100!=2){
字符串responseMessage=connection.getResponseMessage();
抛出新IOException(“HTTP响应代码:“+responseCode+”“+responseMessage”);
}
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
试一试{
StringBuffer StringBuffer=新的StringBuffer();
整数字符;
而((character=bufferedReader.read())!=-1)
{
追加((字符)字符);
tokenSession=stringBuffer.toString();
}
int start=tokenSession.indexOf(“{\'token\:\”)+“{\'token\”:\”.length();
int end=tokenSession.indexOf(“\”}”,start);
String finalToken=tokenSession.substring(开始、结束);
setToken(finalToken);
返回finalToken;
}
最后{
bufferedReader.close();
}
}捕获(协议例外e)
{
字符串msg=e.getMessage();
返回味精;
}捕获(格式错误){
字符串msg=e.getMessage();
返回味精;
}捕获(IOE异常){
字符串msg=e.getMessage();
返回味精;
}
}
}
}

名为token的变量需要什么样的帮助?请正确指定问题。通过查看代码,当变量初始化时,将其设置为null。然后在“storedToken=getToken()处分配一个值;“。您可能期望getToken提供一些值,但返回null。Async表示异步。您的
pat.execute()
调用不会被阻止,下一行将立即执行,在
异步任务
中设置
令牌
之前。确切的问题是,我在名为postAccountTask的嵌套类中剪切令牌值,然后使用属性从嵌套类中获取令牌。当我中断int通过它一切正常,直到我必须调用getToken,然后变量被设置为null。
标记
没有将自身设置为null。只是在尝试使用它之前没有设置它。任何取决于您在
异步任务
中分配给它的值的操作都应该在任务结束时完成,就像在
onP中一样osExecute()
。名为token的变量需要什么样的帮助?请正确指定问题。通过查看代码,当变量初始化时,将其设置为null。然后在“storedToken=getToken()”处分配一个值;“。您可能期望getToken提供一些值,但返回null。Async表示异步。您的
pat.execute()
调用不会被阻止,下一行将立即执行,在
异步任务
中设置
令牌
之前。确切的问题是,我在名为postAccountTask的嵌套类中剪切令牌值,然后使用属性从嵌套类中获取令牌。当我中断int通过它一切正常,直到我必须调用getToken,然后变量被设置为null。
token
没有将自身设置为null。只是在您尝试使用它之前没有设置它。这取决于您在
AsyncTas中分配给它的值