我正在尝试创建登录屏幕并使用 ASyncTask
类。我用它来检查用户名和密码在服务器中是否可用。并且任务返回成功。然后我用它来将变量 loginSuccessfull
的值更改为“true”,并且当我使用 logd 检查它时它会发生变化。但是当我使用它来检查 if 语句时,该值会自动更改为较早的值,这是错误的。这是为什么?
public class LoginScreenActivity extends AppCompatActivity {
EditText usernameEditText;
EditText passwordEditText;
Button loginButton;
public static final String TAG = "LoginScreenActivity";
protected boolean loginSuccessful = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
usernameEditText = findViewById(R.id.username_edit_text);
passwordEditText = findViewById(R.id.password_edit_text);
loginButton = findViewById(R.id.login_button);
final Toast wrongCredentials = Toast.makeText(this, "Wrong username or password", Toast.LENGTH_SHORT);
final LoginScreenBackground background = new LoginScreenBackground();
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
background.execute(usernameEditText.getText().toString(), passwordEditText.getText().toString());
Log.d(TAG, "onClick: " + loginSuccessful);
if(loginSuccessful){
showDeliveryScreen();
}
else{
wrongCredentials.show();
}
}
});
}
class LoginScreenBackground extends AsyncTask<String, Void, String> {
String loginURLString = "http://localhost/login.php";
boolean loginSuccess = false;
public static final String TAG = "BackgroudActivity";
@Override
protected void onPostExecute(String s) {
Log.d(TAG, "onPostExecute: " + s);
if(s.equals("success")){
loginSuccessful = true;
Log.d(TAG, "onPostExecute: " + loginSuccessful);
}
//Log.d(TAG, "onPostExecute: " + this.loginSuccess);
}
@Override
protected String doInBackground(String... strings) {
String responseFromServer = "";
String username = strings[0];
String password = strings[1];
try{
//Setting up the http connection
URL url = new URL(loginURLString);
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
//Setting up the output stream to the php file on the server
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
//setting up the data need to be sent to the server
String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&&" +
URLEncoder.encode("pass", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
//Send data throught the buffered reader
bufferedWriter.write(data);
//close connections
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//Setting up the input stream from the php file on th server
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while((line = bufferedReader.readLine()) != null){
responseFromServer += line;
}
Log.d(TAG, "doInBackground: " + responseFromServer);
}catch (MalformedURLException e){
e.getMessage();
}catch(IOException e){
e.getMessage();
}
return responseFromServer;
}
}
public void showDeliveryScreen(){
Intent intent = new Intent(this, DriverDeliveryActivity.class);
startActivity(intent);
}
}
预期为 true
,但显示的结果为 false
。
最佳答案
实际上并非如此,这对您来说只是一种视觉错觉,在您的 OnPostExecute
中,您没有获得成功响应。
因为您在 OnPostExecute
Log.d(TAG, "onPostExecute: " + s);
还有
Log.d(TAG, "onPostExecute: " + loginSuccessful);
这就是为什么您可能会认为 loginSuccessfull
变量值设置为 true
,但实际上并非如此。
关于java - 我的变量loginSuccessful自动更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56774281/