get server side session id in android using volley
Asked Answered
H

1

5

I am new to android and creating an app that communicate to server through web-service. Server side code is written in asp.net, and server side is maintaining Session. I am Using Volley to call the web-service but the problem i am not able to maintain the session on the server side, as far as i know i have to get the session id from the header i have searched allot on this website but i am not able to succeed.My Problem is to maintain the session throughout the app. Below is my code for calling web-service in android.

_Login_Webservice = new JsonObjectRequest(Request.Method.GET,
            _Login_Url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            // Log.e(Utils.tag, "response :" + response);
            User _User;
            button.setLoadingState(false);

            try {
                boolean Success = response.getBoolean("Success");

                if (Success == true) {

                    //my code


                } 

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("", "exception in Login User Login_Activity: ");
            Log.e("", "error is : " + error.toString());

        }
    }) {


        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            return params;
        }


    };
    //  _Login_Webservice.setRetryPolicy(new DefaultRetryPolicy(5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Adding request to request queue
    Volley_Controller.getInstance().addToRequestQueue(_Login_Webservice,
            Config.VOLEY_TAG);

and the response in the header is below.

{X-AspNet-Version=4.0.30319, X-Android-Selected-Transport=http/1.1, Vary=Accept-Encoding, Date=Thu, 07 Apr 2016 08:26:23 GMT, X-Android-Received-Millis=1460017311847, Set-Cookie=ASP.NET_SessionId=obzjppign5twglksjesm24wi; path=/; HttpOnly, Content-Type=text/JSON; charset=utf-8, X-Powered-By=ASP.NET, X-Android-Response-Source=NETWORK 200, Server=Microsoft-IIS/7.5, X-Android-Sent-Millis=1460017311678, Cache-Control=private}

i need value of ASP.NET_SessionId= and also need to send this in other web-services in header. Right now the response that i am receiving is below

{"Success":false,"Info":"Object reference not set to an instance of an object."}
Hesperian answered 8/4, 2016 at 9:44 Comment(0)
M
8

well You have to use parseNetworkResponse method provided by Volley Library from there you can get the response coming form server in the header.

@Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {

                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers));


                String header_response = String.valueOf(response.headers.values());
               int index1 = header_response.indexOf("ASP.NET_SessionId=");
                int index2 = header_response.indexOf("; path");

                Log.e(Utils.tag, "error is : " + index1 + "::" + index2);

                session_id = header_response.substring(index1, index2);

                // this is your session id put it in the variable and then you can use it any where you want to

                return Response.success(new JSONObject(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));

            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }

when you will call any other web-service just put that sessionId in the header like below

@Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Cookie",SessionId);
            return headers;
        }

remember you have to send ASP.NET_SessionId=obzjppign5twglksjesm24wi as a value and key Cookie

Hope that will help.

Marrowbone answered 8/4, 2016 at 9:53 Comment(1)
This is what I have used #5961332Ease

© 2022 - 2024 — McMap. All rights reserved.