Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. public void login(UserModel data, final UserLoginCallback onCallBack){
  2.         Map<String, Object> map = new HashMap<>();
  3.         map.put("username", data.getUsername());
  4.         map.put("password", data.getPassword());
  5.  
  6.         JSONObject jsonObject = new JSONObject(map);
  7.         String bodyRequest = jsonObject.toString();
  8.         Log.d(myTag, this.getClass().getSimpleName()+">> bodyRequest Login : "+bodyRequest);
  9.  
  10.         JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, loginUrl, bodyRequest, new Response.Listener<JSONObject>() {
  11.             @Override
  12.             public void onResponse(JSONObject response) {
  13.                 try {
  14.                     String msg = response.getString("message");
  15.                     UserModel userModel = new UserModel();
  16.                     JSONObject jsonObject = response.getJSONObject(KEY_LOGIN_RESPONSE);
  17.                     Log.d(myTag, jsonObject.toString());
  18.                     Log.d(myTag, msg+ " login oey");
  19.                     if(jsonObject.has("name")){
  20.                         String name = jsonObject.getString("name");
  21.                         String username = jsonObject.getString("username");
  22.                         String password = jsonObject.getString("password");
  23.                         int userID = jsonObject.getInt("userID");
  24.                         String role = jsonObject.getString("role");
  25.  
  26.                         userModel.setUserID(String.valueOf(userID));
  27.                         userModel.setName(name);
  28.                         userModel.setUsername(username);
  29.                         userModel.setPassword(password);
  30.                         userModel.setRole(role);
  31.                         onCallBack.onSuccess(userModel);
  32.  
  33.                     }
  34.                     else {
  35.                         onCallBack.onFail(msg);
  36.                     }
  37.  
  38.                 } catch (JSONException e) {
  39.                     e.printStackTrace();
  40.                 }
  41.  
  42.             }
  43.         }, new Response.ErrorListener() {
  44.             @Override
  45.             public void onErrorResponse(VolleyError error) {
  46.                 Toast.makeText(context, "Login Failed", Toast.LENGTH_LONG).show();
  47.                 error.printStackTrace();
  48.                 onCallBack.onFail(error.toString());
  49.             }
  50.         });
  51.         jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
  52.                 5000,
  53.                 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  54.                 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  55.         MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest,myTag);
  56.  
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement