Android: Singleton DefaultHttpClient will not keep session
<p>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.</p>

<p>This is the singleton class for the DefaultHttpClient:</p>

<pre><code>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;
  }
}
</code></pre>

<p>I call the client each time it is used in an AsyncTask using Client.getInstance().</p>

<p>I have tried:</p>

<pre><code>ClientInstance = Client.getInstance();
Client.setCurrentResponse(ClientInstance.execute(HttpPost to execute));
</code></pre>

<p>I have tried:</p>

<pre><code>Client.setCurrentResponse(Client.getInstance().execute(HttpPost to execute));
</code></pre>

<p>And numerous other ways of going about it that I have found through searches.</p>

<p>I have also tried using a CookeStore and HttpContext, which produces the same result.</p>

<p>Using consumeContent() on the entity when I get it from the response also does not fix the issue.</p>

<p>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?</p>
