Log in to Android httpclient and use cookies for further processes

I am developing an application that includes activity and the main activity of logging in. If the user first registered, the application saves the username and passes it to sharedPrefs. And at the next start, the login activity uses this username and password, if the server returns true (in xml, getEntity) the main intention of the activity begins. After logging in, I want to interact with the web page using the cookies set in the login login. As far as I browse the internet, they say that I should use the same httpclient so as not to lose cookies. I tried, but could not handle it. So, can I use cookies without using the same httpclient?

The general logic of my application:

httpclient.execute("http://www.abc.com/index.php?process=login&user="+variable1+"&pass="+variable1); //here I get the entity of this response and I parse that return value, after that, if(login==true)--> go on... //here I have to read all page from website which is protected by user authentication(by cookies).(ex:index.php?process=getmymessages) //But I did not manage that. At this point what is your suggestions? 

Thanks in advance!

+4
source share
1 answer

You can use the same HttpClient through the Singleton solution as follows:

 public enum MyAppHttpClient { INSTANCE; private HttpClient configuredHttpClient = null; public HttpClient getConfiguredHttpClient() { if (configuredHttpClient == null) { try { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); configuredHttpClient = new DefaultHttpClient(params); } catch (Exception e) { configuredHttpClient = new DefaultHttpClient(); } } return configuredHttpClient; } } 

You can call MyAppHttpClient.INSTANCE.getConfiguredHttpClient () wherever you need it.

If this is not enough, you can manage the cookies yourself, the BasicCookieStore class is a good starting point, and you can check this stream: Android BasicCookieStore, Cookies and HttpGet

I hope this helps you.

+3
source

All Articles