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