首页 > 解决方案 > 基于来自服务器的状态响应想要在 android 中显示消息

问题描述

嗨,在下面的代码中,我有登录活动。如果用户名和密码通过 json 发送请求。如果用户名和密码正确,则服务器以 json 格式发送 json 响应

在 json 响应中包含关键状态和用户类型。如果状态是成功和用户类型。基于用户类型重定向到特定页面。

但是现在我的问题是,如果服务器以 json 格式发送响应的电子邮件和密码不正确。其中包含一个名为状态的键。

如果状态无效,则希望将消息显示为 toast 格式。

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 ( );
                        }
                        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 ( );
                        }

                        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 ( );
                        }


                    }

                    if (response != null && response.isSuccessful ()&& response.code ()==401)  {

                            status = response.body().getStatus ().toString ( );

                            if (status.equalsIgnoreCase ("Invalid login credentials")) {

                                // dialog.dismiss();
                                makeText (LoginActivity.this, "Invalid login credentials", Toast.LENGTH_SHORT).show ( );
//                            Intent mainIntent;
//                            mainIntent = new Intent (LoginActivity.this, LoginActivity.class);
//                            startActivity (mainIntent);
//                            finish ( );
//                        }
                            }

                    }
                }
//                      else {
//                          if(status.equalsIgnoreCase ("Invalid login credentials"))
//                        makeText(LoginActivity.this, "Invalid EmailId and password", Toast.LENGTH_SHORT).show();
//                        progressDialog.dismiss ();
//                    }

                @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;

标签: androidjson

解决方案


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;

推荐阅读