Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Singleton DefaultHttpClient将不保留会话_Android - Fatal编程技术网

Android:Singleton DefaultHttpClient将不保留会话

Android:Singleton DefaultHttpClient将不保留会话,android,Android,我在Android应用程序中使用DefaultHttpClient的一个单例实例,以便在登录后,与我的网站的会话在多个活动中保持身份验证 这是DefaultHttpClient的单例类: public class Client { private static DefaultHttpClient instance = null; //handler for current response in UI thread after receiving it in AsyncTask pr

我在Android应用程序中使用DefaultHttpClient的一个单例实例,以便在登录后,与我的网站的会话在多个活动中保持身份验证

这是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;
  }
}
每次使用client.getInstance()在异步任务中使用客户端时,我都会调用它

我试过:

ClientInstance = Client.getInstance();
Client.setCurrentResponse(ClientInstance.execute(HttpPost to execute));
Client.setCurrentResponse(Client.getInstance().execute(HttpPost to execute));
我试过:

ClientInstance = Client.getInstance();
Client.setCurrentResponse(ClientInstance.execute(HttpPost to execute));
Client.setCurrentResponse(Client.getInstance().execute(HttpPost to execute));
还有我通过搜索找到的许多其他方法

我还尝试使用CookeStore和HttpContext,这会产生相同的结果

当我从响应中获取实体时,在该实体上使用consumeContent()也无法解决此问题


我所发现的一切都表明,使用DefaultHttpClient的单例实例允许它维护其会话,但每次我执行post以执行我的网站要求用户登录的操作时,我得到的响应正文显示错误,指示未设置“user_id”会话变量(所以用户没有登录)。我已经彻底搜索了Google和Stack Overflow,但没有结果。有人能指出我做错了什么吗?

对不起,各位


在几乎失去理智之后,我又看了一遍我的php代码。结果发现问题出在我的网页上-我在收到移动HttpPost时不允许php调用session_start()。谁知道呢,也许我的问题/答案会帮助其他人。

谢谢你把答案放在这里!