问题
I'm making an login page.What i have done is-->
1.When user typed right login details -->it will take him to next successful home page
2.When user typed wrong login details-->displayed an error response from API that email or password is wrong try again later..
So basically what I want is--> When user forgot to fill field like email or password, it should give response from API like "data is missing".
Here is my json:
{
"status": 400,
"message": "Data missing.",
"user_msg": "Data missing"
}
Here is my code that I have done so far-->
loginbtn.setOnClickListener {
val email = loginuser.text.toString().trim()
val password = loginpassword.text.toString().trim()
if (email.isEmpty()) {
loginuser.error = "Email required"
loginuser.requestFocus()
return@setOnClickListener
}
if (password.isEmpty()) {
loginpassword.error = "Password required"
loginpassword.requestFocus()
return@setOnClickListener
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
// when typed right details
SharedPrefManager.getInstance(applicationContext)
.saveUser(response.body()?.data!!)
val intent = Intent(applicationContext, HomeActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
Log.d("kjsfgxhufb",response.body()?.status.toString())
startActivity(intent)
finish()
}
else
{
//when typed wrong details
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
So I want to know the logic for above json when user forgot to fill any of the field like email or password. So that it must show like data is missing in toast.
Need help ...Thanks
回答1:
First check if the fields are empty then api call must not be made and if the field data is relevent only then you should make an api call. For e.g
// make a validation check if the field(in this case edittext) is empty or not
private boolean validate(EditText editText) {
// check the lenght of the enter data in EditText and give error if its empty
if (editText.getText().toString().trim().length() > 0) {
return true; // returs true if field is not empty
}
editText.setError("Please Fill This");
editText.requestFocus();
return false;
}
// then wherever you want to do the api call first validate the fields that it is empty or not
// validate the fields and call sign method to implement the api
if (validate(field1) && validate(field2) && validate(field3)) {
yourApiMethodCall();
}
I hope it helps you
回答2:
Instead of checking after request is done, you can validate it before request is done
For example,
void checkValidation()
{
if(username.getText().equals("")){
//show appropriate message like
username.setError("Please enter username");
return false;
}
if(password.getText().equals("")){
//show appropriate message like
username.setError("Please enter username");
return false;
}
return true;
}
and
if(checkValidation){
//Make request here
}
If you really want in response, you'll have to code it on server side.
回答3:
As you got response from the server do one thing:
- Check the all the inputfields length, verify is any field is missed by user.
- Prompt Toast OR dialog whatever field is missing.
来源:https://stackoverflow.com/questions/62508345/how-to-display-field-is-missing-error-response-from-api-using-retrofit