This is a simple tip for handling sessions in android applications which communicates with server for retrieving information. Created an instance of DefaultHttpClient in the first Activity when connection established.
public static DefaultHttpClient httpClient;
Initialize it for the first time as follows.
URL url=new URL(urlToHit); LoginScreen.httpClient = new DefaultHttpClient(); //LoginScreen is the name of the current Activity
HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost);
Now for all further connections I used the same httpClient For example in the next activity.
URL url=new URL(urlToHit); HttpPost httppost = new HttpPost(url.toString()); HttpResponse response = LoginScreen.httpClient.execute(httppost);
This is a simple but useful method for handling http sessions in android.
Comments
Extraordinarily educative thank you, I believe your trusty readers may want even more information such as this carry on the excellent effort.
Thnx Buddyyy...It Helps a Lot
Thnx Buddyyy...It Helps a Lot...
Thats cool. Good to hear that
Thats cool. Good to hear that..
Can you please send me a
Can you please send me a sample code for using http sessions in my android app..Please send to ganeshkumar020@gmail.com
How do you persist the
How do you persist the cookies associated with the session? Serialize it and put it in sharedPrefrencesand once your app gets garbage collected you reconstruct the cookies and attach it to the httpClient? ... Or can we persist the httpClient somehow?
Add new comment