Session persistence for Android

Source: Internet
Author: User

In a recently written Android system, you need to request data from the Web server. A login activity will be sent to the mainactivity after logon. Both the logon and mainactivity need to request the jsonapi of PHP, so we have to keep the session in the network request for a long time. In fact, sesion is directly transmitted through a cookie named "sessionid" in the browser and web server, therefore, the web session can be used as long as the sessionid remains the same during each data request. In this way, the sessionid value is obtained and saved in a static variable during the first data request, then, when the second request is sent, the sessionid should be put together in the cookie and sent to the server. The server uses this sessionid to identify whether the client is requesting data, in PHP, The sessionid is called PHPSESSID. Paste the following code

Import java. Io. ioexception;
Import java. Io. unsupportedencodingexception;
Import java. util. List;

Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. httpstatus;
Import org. Apache. http. namevaluepair;
Import org. Apache. http. Client. clientprotocolexception;
Import org. Apache. http. Client. cookiestore;
Import org. Apache. http. Client. entity. urlencodedformentity;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. Cookie. Cookie;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. Protocol. HTTP;
Import org. Apache. http. util. entityutils;

Public class myhttpclient implements inetconfig {
Private defaulthttpclient httpclient;
Private httppost;
Private httpentity;
Private httpresponse;
Public static string PHPSESSID = NULL;
Public lvhttpclient (){

}

Public String ExecuteRequest (string path, list <namevaluepair> Params ){
String ret = "NONE ";
Try {
This. httppost = new httppost (basepath + path );
Httpentity = new urlencodedformentity (Params, HTTP. utf_8 );
Httppost. setentity (httpentity );
// The first time is generally not assigned a value. If there is a value, send the sessionid to the server.
If (null! = PHPSESSID ){
Httppost. setheader ("cookie", "PHPSESSID =" + PHPSESSID );
}
Httpclient = new defaulthttpclient ();
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
}
Try {
Httpresponse = httpclient.exe cute (httppost );
If (httpresponse. getstatusline (). getstatuscode () = httpstatus. SC _ OK ){
Httpentity entity = httpresponse. getentity ();
Ret = entityutils. tostring (entity );
Cookiestore mcookiestore = httpclient. getcookiestore ();
List <cookie> cookies = mcookiestore. getcookies ();
For (INT I = 0; I <cookies. Size (); I ++ ){
// Here, the value of the cookie ['phpsessid '] is read in a static variable to ensure that the value is the same at a time.
If ("PHPSESSID". Equals (cookies. Get (I). getname ())){
PHPSESSID = cookies. Get (I). getvalue ();
Break;
}

}
}
} Catch (clientprotocolexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}

Return ret;
}
}

In fact, the Web principles are the same. It is based on the HTTP protocol. If the website does not use PHP, the cookie called sessionid may be called another one, and it is not PHPSESSID, it is called another name, which may need to be checked according to the specific situation.

In fact, it is not just an android program. When other programs need to use this function, they only need to add the corresponding sessionid in the HTTP request header. This method can help you understand sessionid. In fact, if it is more common, you can send all the cookies back to the server every time, this can solve the session persistence problem, but it may slightly increase the network traffic overhead.

Here we can see the essence of a sessionid. Mark it by the way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.