所以我正在android工作室中为一个具有登录功能的令人兴奋的网页构建一个android应用程序。我已经成功地将应用程序连接到网站数据库,我可以登录并从服务器获得响应。应用程序正在响应中查找错误值,但响应不包含错误值。
这是我的应用程序中的代码:
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
这是登录成功时服务器给出的响应:
Login Response: {"howislife":1,"rekeningnummer":"212220","soortuser":"student","datum":"05 Sep 2017"}
因此,当登录成功时,它会显示“howislife:1”,当密码或用户名不正确时,它会显示“howislife:2”或“howislife:3”。所以我的android应用程序中的代码需要检查“howislife”的值。我该怎么做?提前谢谢!
最佳答案
尝试此代码
int howislife = (!jsonObjects.isNull("howislife")) ?
jsonObjects.optInt("howislife") : 0;
if (howislife == 1) {
//login sucess
} else {
//login failed
}