2

I need to maintain a session for below REST API call from Android app. I'm able to call these API using volley, But the session is not being maintained. I first call validateUser API to validate the user and store his email in session. Then I need to call getDashboardDetails to fetch user details, But email in session object is null

// Spring REST code

@RequestMapping(value = "/validateUser", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<UserVO> validateUser(@RequestBody UserVO userVO, HttpSession session, HttpServletResponse response) {

    HttpStatus httpStatus = HttpStatus.OK;
    validatedUserVo = userService.getUserDetails(userVO);

    session.setAttribute("email", validatedUserVo.getEmail());

    return new ResponseEntity<UserVO>(validatedUserVo, httpStatus);
}

@RequestMapping(value = "/getDashboardDetails", method = RequestMethod.GET)
public ResponseEntity<List<DashBoardVO>> getDashboardDetails(HttpSession session) {

    List<DashBoardVO> dashBoardVOList = null;
    HttpStatus httpStatus = HttpStatus.OK;
    String email = (String)session.getAttribute("email");
    if(email!=null){
        dashBoardVOList = dashboardService.getDashboardDetails(email);
    }
    else{
        httpStatus = HttpStatus.BAD_REQUEST;
    }
    return new ResponseEntity<List<DashBoardVO>> (dashBoardVOList, httpStatus);
}

// validateUser call in LoginActivity

json.put("email",username1.getText().toString());
json.put("password",password1.getText().toString());
JsonObjectRequest login_data = new 
JsonObjectRequest(Request.Method.POST, url, json,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            StringBuffer s = null;
            tv.setText(response.toString());
            edit.putBoolean("login_success",true);
            edit.commit();
            Intent i = new Intent(LoginActivity.this,DashboardActivity.class);
            try {
                i.putExtra("name",response.getString("name"));
                i.putExtra("role",response.getString("role"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            startActivity(i);
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
                  tv.setText(error.toString());

        }
    });
RequestQueue rq = Volley.newRequestQueue(this);
rq.add(login_data)

//sharedPreferences in LoginActivity

boolean b = sharedPreferences.getBoolean("login_success",false);
    if(b){
        Intent i = new 
Intent(LoginActivity.this,DashboardActivity.class);
        startActivity(i);
    }

// getDashboardDetails call in DashboardActivity

JsonArrayRequest j1 = new JsonArrayRequest(Request.Method.GET, url, 
null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
    try {
        JSONObject temp = response.getJSONObject(1);
        tv.setText(temp.getString("id"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
        tv.setText(error.toString());
}
});
RequestQueue rq = Volley.newRequestQueue(this);
rq.add(j1);

How do I use/maintain session between different api calls? in android app

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Hemanshu Belani
  • 199
  • 3
  • 13

0 Answers0