我已经成功地在我的 sharepreference 中检索并存储了一个 cookie。使用下面的 post 方法:
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());
现在我需要使用另一个 url 和存储在 sharepreference 中的 cookie 再次调用 webservice。如何使用附加了此 cookie 的 http 进行呼叫,以及如何在我的应用程序中保持 cookie 处于活动状态。