Skip to main content
added 3 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

My simple android Simple Android Volley login request

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

My simple android Volley login request

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

Simple Android Volley login request

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?

edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Source Link

My simple android Volley login request

I've made a simple login api request which as response gets token. I had to use Toast 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);
}