0

I'm using volley to send requests to my web server. First I post a username and password to a url like /login/. Second I want to get the information of that user, which can be obtained via url like /getUser. the first request is finished with 200 status code and sends back a response indicating successful login, but when I'm trying to send the second request in onResponse callback of the first request, it appears that the user is no longer logged in.

I also tested the web server with postman and it works properly.

Arya Mh
  • 3
  • 2
  • You are not managing your sessions. – Anis LOUNIS aka AnixPasBesoin Jan 31 '17 at 22:14
  • In the web server I handle sessions, as I said it works properly when I use postman to send requests, if you mean I have to manage sessions in Volley, would you please explain how? – Arya Mh Feb 01 '17 at 08:00
  • I'm affraid volley doesn't take care of seesions for you. If you're not using cookies, you might add the session id to your requests every time you send one, if you're using cookies it's some more work to do, better check this link http://stackoverflow.com/questions/16680701/using-cookies-with-android-volley-library – Anis LOUNIS aka AnixPasBesoin Feb 01 '17 at 08:11

1 Answers1

0

You need to generate tokens to authenticate your API requests. On successful login, let the backend create a token for the authenticated user and send it back to the android app. You can store the token in your sqlite database so that you use it to authenticate all the other subsequent requests like this

JSONObject userObj = new JSONObject(response);
userSession.initializeSession(userObj.getString("names"), email, userObj.getString("token"));

For all other subsequent request to have to pass the token as shown below in volley.

     public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + session.getToken());
            return headers;
        }
phang
  • 518
  • 4
  • 20