Wednesday, 27 June 2018

Android - Volley library: Session expired issue in SSL production environment

I had 2 pages: first one is login page and second is category page. In login API after entering the credentials, I am getting the response as sesssion id from response header. The sesssion id will be saved and it will use for further API calls. I am trying to call second API (category page). In this page, as an input am passing the saved session id in the request header. Getting response as "session expired". Also tried to pass Set-Cookie: PHPSESSID=d9f9sdkfjs9 in the request header. but it didn't work.

Note :

  1. I am experiencing this issue in production environment only (SSL included)
  2. I am using volley library for handling APIs.


public void fnCallLoginAPI() {
    try {
        //DEMO URL
        //final String URL="http://demo.io/api/api.php?m=login";
        //LIVE URL
        final String URL = "https://www.live.com/shop/api/api.php?m=login";
        final String requestBody = "email=abc.b@xyz.com" + "&password=43443==" + "&strPlatform=i" + "&strDeviceToken=null";
              StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                String strResponse = response;
                System.out.println("THE RESPONSE IS in PROFILE IS" + response);
                  }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                })
        {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Cookie", "PHPSESSID=" + sessionID);
                return headers;
            }
 @Override
            public byte[] getBody() throws AuthFailureError {
                byte[] body = new byte[0];
                try {
                    System.out.println("THE REQIEST BODY IS" + requestBody);
                    body = requestBody.getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    Log.e("TAG", "Unable to gets bytes from JSON", e.fillInStackTrace());
                }
                return body;
            }
        };

        AppApplication.getInstance().addToRequestQueue(stringRequest, "assignment");
    } catch (Exception e) {

    }
}


public void fnCallCateGoryAPI(){
try { final String URL ="https://www.live.com/shop/api/api.php?m=getcategories"; 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            String strResponse = response;
            System.out.println("THE RESPONSE IS in PROFILE IS" + response);
            JSONObject jsonObj = null;
            try {
                jsonObj = new JSONObject(strResponse);
                sessionID = jsonObj.optString("session_id");
                System.out.print("sessionID" + sessionID);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            })
    {
      };

    AppApplication.getInstance().addToRequestQueue(stringRequest, "assignment");
} catch (Exception e) {}
}}



from Android - Volley library: Session expired issue in SSL production environment

No comments:

Post a Comment