So I'm building an Android app in Android Studio for an excisting webpage with a login feature. I connected the app succesfully to the websites database already and I can login and get a response from the server. The app is looking for an error value in the response, but the response contains no error value.
This is the code in my app:
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();
This is the response the server gives when the login was succesfull:
Login Response: {"howislife":1,"rekeningnummer":"212220","soortuser":"student","datum":"05 Sep 2017"}
So when the login was succesfull it says "howislife:1", when the password or username is incorrect it says "howislife:2" or "howislife:3". So the code in my Android app needs to check what value "howislife" is . How can I do that? Thanks in advance!
Try this code,
int howislife = (!jsonObjects.isNull("howislife")) ?
jsonObjects.optInt("howislife") : 0;
if (howislife == 1) {
//login sucess
} else {
//login failed
}
I assume your api gives response always with those parameters and if the response comes empty or null then check for it before converting to JSONObject , then you could try below code, it will work.
JSONObject jObj = new JSONObject(response);
if(jObj.getInt("howislife")==1){
System.out.println("Login success");
}else{
System.out.println("Login fail");
}
There is a function "has()" to check keys for JSONObject in Android.
boolean has (String name) Returns true if this object has a mapping for name. The mapping may be NULL.
More details:
https://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
I solved it using this code already, thanks!
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
int login1 = jObj.getInt("howislife");
System.out.println(login1);
//Check for error node in json
if (login1 == 1) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else if(login1 == 2) {
Toast.makeText(getApplicationContext(), "Wachtwoord verkeerd", Toast.LENGTH_LONG).show();
} else if(login1 == 3) {
Toast.makeText(getApplicationContext(), "Gebruikersnaam of/en wachtwoord verkeerd", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Er is iets fout gegaan, probeer opnieuw.", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.