在httprequest android上保持应用程序与cookie同步
我已成功地在sharepreference中检索并存储了一个cookie。使用以下post方法:在httprequest android上保持应用程序与cookie同步,android,Android,我已成功地在sharepreference中检索并存储了一个cookie。使用以下post方法: public static void postData() throws JSONException { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost
public static void postData() throws JSONException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constant.CONNECTION_URL);
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login",""));
nameValuePairs.add(new BasicNameValuePair("pass",""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost,resonseHandler);
JSONObject obj = new JSONObject(response);
success = obj.get("success").toString();
fname=obj.get("fname").toString();
if (success.equalsIgnoreCase("1")){
//connection successfull
login_success_status.SavePreferences("login_success_status", success);
login_success_fname.SavePreferences("login_success_fname",fname);
List<Cookie> cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();
CookieSyncManager.getInstance().sync();
cookie_name.SavePreferences("cookie_name",cookies.toString());
publicstaticvoidpostdata()抛出JSONException{
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(常量.CONNECTION\uURL);
ResponseHandler resonseHandler=新的BasicResponseHandler();
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“登录”,“登录”);
添加(新的BasicNameValuePair(“pass”,“pass”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
response=httpclient.execute(httppost,resonseHandler);
JSONObject obj=新的JSONObject(响应);
success=obj.get(“success”).toString();
fname=obj.get(“fname”).toString();
if(success.equalsIgnoreCase(“1”)){
//连接成功
登录成功状态。保存首选项(“登录成功状态”,成功);
login\u success\u fname.SavePreferences(“login\u success\u fname”,fname);
列出cookies=((AbstractHttpClient)httpclient.getCookieStore().getCookies();
CookieSyncManager.getInstance().sync();
cookie_name.SavePreferences(“cookie_name”,cookies.toString());
现在,我需要使用另一个url和存储在sharepreference中的cookie对webservice进行另一次调用。我如何在连接了此cookie的情况下使用http进行调用,以及如何在我的应用程序中保持cookie的活动状态。当您登录到服务器时,会发生什么情况,在客户端和服务器之间创建一个会话,您将获得该会话n Cookie中的id。此会话id在一段时间内有效,这段时间由服务器在您从应用程序注销后设置。此会话已过期,在下次呼叫时不再有效。如果您使用网络银行,您可能知道在登录后您何时停留一段时间。当您在一段时间后单击任何选项时,它将表明您的会话已过期e并重定向到登录页面。一旦您退出应用程序,同样的情况也会出现。以前的cookie一旦过期就不再有用。因此,将cookie持久存储在SharedReferences中没有任何好处 1) 将
列表cookie声明为公共静态
public static List<Cookie> cookies ;
你是否需要在整个应用程序中使用相同的Cookie
?是的,我需要在另一个http调用中使用此Cookie将你的Cookie存储在应用程序
类中,然后在你想使用它们时检索。一旦你关闭应用程序,它将不再可用,但当你下次启动应用程序时,请在后台调用你的web服务nd获取它们并再次将它们存储在应用程序
类中。cookie是否为应用程序自动存储?如果我关闭应用程序并再次打开它,cookie是否仍将保持存储状态?在您的情况下,cookie不需要持久存储,因为应用程序处于活动状态时我们需要相同的cookie,我指的是登录会话在web上处于活动状态,因此一旦应用程序关闭,cookie将过期,并且在您重新启动应用程序时将不再有用。如果我想使其持久化,我该怎么办?Nikita您不明白我的意思是,此处不要求持久存储cookie,因为一旦您从应用程序注销,服务器将不会接受相同的cookie,因为一旦您退出/注销应用程序,服务器将删除该cookie,并且该cookie对下一次调用无效。因此,请尝试此解决方案。该解决方案正在与我完美配合。如果我关闭应用程序并稍后重新打开,cookie是否仍在应用程序中
HttpClient httpclient = new DefaultHttpClient();
if (cookies != null) {
int size = cookies.size();
for (int i = 0; i < size; i++) {
httpclient.getCookieStore().addCookie(cookies.get(i));
}
}
HttpPost httppost = new HttpPost(Constant.CONNECTION_URL);
ResponseHandler<String> resonseHandler = new BasicResponseHandler();
if (success.equalsIgnoreCase("1")){
//connection successfull
login_success_status.SavePreferences("login_success_status", success);
login_success_fname.SavePreferences("login_success_fname",fname);
cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();