Get server side session id in android using volley

I am new to android and am creating an application that communicates with the server through a web service. The server side code is written in asp.net, and the server side maintains the session. I use Volley to call a web service, but the problem is I can’t support the server side session, as far as I know, I need to get the session ID from the header that I have searched on this website, but I can’t succeed. My problem is to maintain a session throughout the application . Below is my code for calling a 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 answer is in the header 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 the value ASP.NET_SessionId = , and also send it to other web services in the header. Right now the answer I get is below

 {"Success":false,"Info":"Object reference not set to an instance of an object."} 
+5
android session android-volley
Apr 08 '16 at 9:44
source share
1 answer

Well, you should use the parseNetworkResponse method provided by the Volley library, from where you can get the response 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 call any other web service just put this sessionId in the header as below

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

remember that you need to send ASP.NET_SessionId = obzjppign5twglksjesm24wi as the value and cookie key

Hope this helps.

+7
Apr 08 '16 at 9:53 on
source share



All Articles