0
\$\begingroup\$

I've made a simple login API request which gets a token as a response. I had to use Toast a few times. Is is possible to make it with only two Toasts Successful/Failed?

public void login(View view){
    RequestQueue queue = Volley.newRequestQueue(this);
    String URL = "http://10.0.2.2:8000/api/login";
    StringRequest request = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObj = new JSONObject(response);
                        myToken =jsonObj.getString("token");
                        if (myToken != ""){
                            Toast.makeText(getApplicationContext(), "Login successfull !", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Ooops something is not correct !", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                         myToken = "";
                        Toast.makeText(getApplicationContext(), "Ooops something is not correct !", Toast.LENGTH_LONG).show();
                         e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener()
                {
                @Override
                public void onErrorResponse(VolleyError error) {
                    myToken = "";
                    Toast.makeText(getApplicationContext(), "Ooops something is not correct !", Toast.LENGTH_LONG).show();
                    NetworkResponse response = error.networkResponse;
                    String errorMsg = "";
                    if(response != null && response.data != null){
                        String errorString = new String(response.data);
                    }
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String> params = new HashMap<String, String>();
            TextInputEditText mail = findViewById(R.id.textInputEditTextEmail);
            TextInputEditText pw = findViewById(R.id.textInputEditTextPassword);
            params.put("email", mail.getText().toString());
            params.put("password", pw.getText().toString());
            return params;
        }
    };
    queue.add(request);
}
\$\endgroup\$
1
\$\begingroup\$

You can use a variable and assume that it will fail, if you do not print the new message

   public void login(View view){
            RequestQueue queue = Volley.newRequestQueue(this);
            String URL = "http://10.0.2.2:8000/api/login";
            StringRequest request = new StringRequest(Request.Method.POST, URL,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response) {
                            String loginMsg = "Ooops something is not correct !";

                            try {
                                JSONObject jsonObj = new JSONObject(response);
                                myToken =jsonObj.getString("token");
                                if (myToken != ""){
                                    loginMsg = "Login successfull !"
                                }
                            } catch (JSONException e) {
                                 myToken = "";    
                                 e.printStackTrace();
                            }

                            Toast.makeText(getApplicationContext(), loginMsg, Toast.LENGTH_LONG).show();

                        }
                    },
                    new Response.ErrorListener()
                        {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            myToken = "";
                            Toast.makeText(getApplicationContext(), "Ooops something is not correct !", Toast.LENGTH_LONG).show();
                            NetworkResponse response = error.networkResponse;
                            String errorMsg = "";
                            if(response != null && response.data != null){
                                String errorString = new String(response.data);
                            }
                        }
                    }
            ) {
                @Override
                protected Map<String, String> getParams()
                {
                    Map<String, String> params = new HashMap<String, String>();
                    TextInputEditText mail = findViewById(R.id.textInputEditTextEmail);
                    TextInputEditText pw = findViewById(R.id.textInputEditTextPassword);
                    params.put("email", mail.getText().toString());
                    params.put("password", pw.getText().toString());
                    return params;
                }
            };
            queue.add(request);
        }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.