我用lamp(linux + apache + mysql + php)制作了一个网络服务器。我想设置cookie来保存用户信息,如用户ID,用户名。所以load.php是这样的:
....
///login successful
session_start();
$_COOKIE["userid"]=$userid;
$response["success"]=1;
$response["message"]="load successful";
echo json_encode($response);
...
在 Android LoadActivity 中,我创建了一个扩展 AsyncTask 的类 WebLoader,并String doInBackground(String... params)
像这样覆盖该函数:
client=new DefaultHttpClient();
HttpPost post=new HttpPost(url_up);
...
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
CookieStore mCookieStore = client.getCookieStore();
List<Cookie> cookies = mCookieStore.getCookies();
if(cookies.isEmpty())
Log.d("cookie","cookie is none");
for (int i = 0; i < cookies.size(); i++) {
if("userid".equals(cookies.get(i).getName()))
{
userid=Integer.parseInt(cookies.get(i).getValue());
}
}
当我运行应用程序时,logcat 说:cookie is none。为什么 Cookie 没有?我已经在 load.php 中设置了它。