Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void login(UserModel data, final UserLoginCallback onCallBack){
- Map<String, Object> map = new HashMap<>();
- map.put("username", data.getUsername());
- map.put("password", data.getPassword());
- JSONObject jsonObject = new JSONObject(map);
- String bodyRequest = jsonObject.toString();
- Log.d(myTag, this.getClass().getSimpleName()+">> bodyRequest Login : "+bodyRequest);
- JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, loginUrl, bodyRequest, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- String msg = response.getString("message");
- UserModel userModel = new UserModel();
- JSONObject jsonObject = response.getJSONObject(KEY_LOGIN_RESPONSE);
- Log.d(myTag, jsonObject.toString());
- Log.d(myTag, msg+ " login oey");
- if(jsonObject.has("name")){
- String name = jsonObject.getString("name");
- String username = jsonObject.getString("username");
- String password = jsonObject.getString("password");
- int userID = jsonObject.getInt("userID");
- String role = jsonObject.getString("role");
- userModel.setUserID(String.valueOf(userID));
- userModel.setName(name);
- userModel.setUsername(username);
- userModel.setPassword(password);
- userModel.setRole(role);
- onCallBack.onSuccess(userModel);
- }
- else {
- onCallBack.onFail(msg);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(context, "Login Failed", Toast.LENGTH_LONG).show();
- error.printStackTrace();
- onCallBack.onFail(error.toString());
- }
- });
- jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
- 5000,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- MySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest,myTag);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement