因此,我正在 Android Studio 中为具有登录功能的现有网页构建 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
}
关于java - 检查 JSON 响应是否包含特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050391/