嗨,在下面的代码中我具有登录活动。如果用户名和密码通过json发送请求,如果用户名和密码正确,则服务器以json格式发送json响应
在json中,响应包含关键状态和用户类型。如果状态为成功和用户类型,则根据用户类型重定向到特定页面。
但是现在我的问题是,如果服务器以json格式发送响应时电子邮件和密码不正确。其中包含一个名为status的密钥。
如果状态无效,则希望以烤面包格式显示消息。
return redirect()->back();
答案 0 :(得分:0)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
API service = retrofit.create(API.class);
try{
JSONObject parmobject=new JSONObject ();
parmobject.put("emailMobile",emailMobile);
parmobject.put("password",password);
Call<Login> userCall = service.getUser(parmobject.toString ());
userCall.enqueue(new Callback<Login> () {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
if (response != null && response.code ()==200) {
status = response.body ( ).getStatus ( ).toString ( );
usertype = response.body ( ).getUsertype ( ).toString ( );
if (status.equalsIgnoreCase ("success") && usertype.equalsIgnoreCase ("admin")) {
// dialog.dismiss();
makeText (LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show ( );
Intent mainIntent;
mainIntent = new Intent (LoginActivity.this, NavigationViewActivity.class);
startActivity (mainIntent);
finish ( );
}
else if (status.equalsIgnoreCase ("success") && usertype.equalsIgnoreCase ("operator")) {
// dialog.dismiss();
makeText (LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show ( );
Intent mainIntent;
mainIntent = new Intent (LoginActivity.this, OperatorHome.class);
startActivity (mainIntent);
finish ( );
}
else if (status.equalsIgnoreCase ("success") && usertype.equalsIgnoreCase ("employee")) {
// dialog.dismiss();
makeText (LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show ( );
Intent mainIntent;
mainIntent = new Intent (LoginActivity.this, EmployeeHome.class);
startActivity (mainIntent);
finish ( );
}
else if (status.equalsIgnoreCase ("invalid"))
{
makeText (LoginActivity.this, status, Toast.LENGTH_SHORT).show ( );
}
else
{
makeText (LoginActivity.this, "Invalid login credentials", Toast.LENGTH_SHORT).show ( );
}
}
else
{
makeText (LoginActivity.this, "Invalid login credentials", Toast.LENGTH_SHORT).show ( );
}
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Some error occurred -> ", Toast.LENGTH_LONG).show();;
progressDialog.dismiss ();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
return retrofit;