سؤال

I'm looking for ability of holding session using aQuery, that starts from first HTTP request. I'm getting cookies and storing them, but how to add them to new request, so server will know that same user with same session trying to reach webservice?

I tried something like that, before ajax request (cookie holds Cookie object, callback holds AjaxCallback object):

Map<String, String> cookieMonster = new HashMap<String, String>();
cookieMonster.put("domain", cookie.getDomain());
cookieMonster.put("version", "" + cookie.getVersion());
cookieMonster.put("path", cookie.getPath());
cookieMonster.put("name", cookie.getName());
cookieMonster.put("value", cookie.getValue());
cookieMonster.put("expiry", cookie.getExpiryDate().toGMTString());

Then

callback.cookies(cookieMonster);

I also set

AjaxCallback.setReuseHttpClient(true);

But looks like there are some problem with this in android query, and it not behaves as I expected.

Anyone solved this issue? I would use sending token in each request as param, but I need counting session properly, not recreate them in each request.

Thanks in advance for any help!

Regards, David

UPDATE

In fact I was quite close to solution. Here is working method that I use before another callback where static method getCookie holds list of cookies from previous callback (I use UserData.setCookie(status.getCookie() to save it from previous callback).

private void setCookie(AjaxCallback<JsonElement> callback) {

        try {
            List<Cookie> cookies = UserData.getCookie();
            for (Cookie cookie : cookies) {

                Map<String, String> cookieMonster = new HashMap<String, String>();
                cookieMonster.put("domain", cookie.getDomain());
                cookieMonster.put("version", "" + cookie.getVersion());
                cookieMonster.put("path", cookie.getPath());
                cookieMonster.put("name", cookie.getName());
                cookieMonster.put("value", cookie.getValue());

                if (cookie.getExpiryDate() != null) {

                    String DATE_FORMAT = "EEE, dd-MMM-yyyy HH:mm:ss z";
                    final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
                    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                    String dateTimeString = sdf.format(cookie.getExpiryDate());

                    cookieMonster.put("expiry", dateTimeString);
                }

                callback.cookie(cookie.getName(), cookie.getValue());

                Log.d("Set cookie from UserData", cookie.toString());
            }
        } catch (Exception e) {
            Log.d("Aq setCookie problem", "message: " + e.getMessage());
        }
    }
هل كانت مفيدة؟

المحلول

Thanks for your research, I was looking for the same issue because I want to keep a PHP session. I think AQuery should be able to handle the cookies by it self and I still don't understand why we must set the cookies in the callback and not in the AQuery instance. Otherwise we should be forced to use the AjaxCallback anytime we need to handle the cookies :-/

By the way, here is a another sample based on your research. First we need a variable to save the cookie info:

List<Cookie> cookies;

Next we make the JSON call:

AQuery query = new AQuery(this);
query.ajax("your url", JSONObject.class, new AjaxCallback<String>() {
    @Override
    public void callback(String url, String json, AjaxStatus status) { 
        cookies = status.getCookies(); // We are saving the cookies in our variable
    }
});

After we save the cookie data we could use it in the next requests:

AQuery query = new AQuery(this);
AjaxCallback<JSONObject> ajaxCallback = new AjaxCallback<JSONObject>() {
    @Override
     public void callback(String url, String json, AjaxStatus status) { 
          cookies = status.getCookies(); // Save the cookies in every requests you made
    }
}
for (Cookie cookie : cookies) {
     ajaxCallback.cookie(cookie.getName(), cookie.getValue()); 
     //Here we are setting the cookie info.
}
query.ajax("your url", JSONObject.class, ajaxCallback);

Thanks again poslinski.

نصائح أخرى

This will not work for images as the there is then instantiated BitmapAjaxCallback in code directly...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top