I am using a singleton instance of a DefaultHttpClient in my Android application so that the session with my website remains authenticated across multiple activities after logging in.
This is the singleton class for the DefaultHttpClient:
public class Client {
private static DefaultHttpClient instance = null;
//handler for current response in UI thread after receiving it in AsyncTask
private static HttpResponse currentResponse = null;
//I also tried this without "synchronized"
public synchronized static DefaultHttpClient getInstance()
{
if(instance == null)
{
instance = new DefaultHttpClient();
}
return instance;
}
public static void setCurrentResponse(HttpResponse response)
{
currentResponse = response;
}
public static HttpResponse getCurrentResponse()
{
return currentResponse;
}
}
I call the client each time it is used in an AsyncTask using Client.getInstance().
I have tried:
ClientInstance = Client.getInstance();
Client.setCurrentResponse(ClientInstance.execute(HttpPost to execute));
I have tried:
Client.setCurrentResponse(Client.getInstance().execute(HttpPost to execute));
And numerous other ways of going about it that I have found through searches.
I have also tried using a CookeStore and HttpContext, which produces the same result.
Using consumeContent() on the entity when I get it from the response also does not fix the issue.
Everything I have found has said that using a singleton instance of DefaultHttpClient allows it to maintain its session, but every time I execute a post to perform an action for which my website requires users to be logged in, the response body I get back displays the error indicating that the 'user_id' session variable is not set (so the user is not logged in). I have searched Google and Stack Overflow exhaustively to no avail. Can somebody please point out what I'm doing wrong?
Sorry for the question, guys.
After almost losing my sanity, I looked my php code over one more time. It turns out the problem was on my webpage - I was not allowing the php to call session_start() when receiving the mobile HttpPost. Who knows, maybe my question/answer will help somebody else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With