[Answered ]-Django session id on android

2👍

TO achieve this you need to maintain session. And to maintain session there is two posibilities

1) Use a single DefaultHttpClient for all request.

this approach will not work if you want to call multiple request simultaneously.

2) Sync common cookies among multiple DefaultHttpClient connections.

public static List<Cookie> cookies;

Read session cookies after login.

HttpResponse WSresponse = httpclient.execute(httppost);


                try {
                    cookies = httpclient.getCookieStore().getCookies();
                    sync();
                } catch (Exception e) {
                }

public static DefaultHttpClient getHttpclient() {

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, timeOut);
        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

        if (cookies != null) {
            int size = cookies.size();
            for (int i = 0; i < size; i++) {
                httpclient.getCookieStore().addCookie(cookies.get(i));
            }
        }
        return httpclient;
    }

UPDATE:

public void sync() {
        if (cookies != null) {

            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            for (Cookie cookie : cookies) {

                Cookie sessionInfo = cookie;
                String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
                cookieManager.setCookie("http://yourdomain.com", cookieString);
                CookieSyncManager.getInstance().sync();
            }
        }
    }

This sync() method is use to manage session for httpclient and webview/browser.That is if you want to perform something like you are log-in from the native application and want to some specific operation on webview which is required log-in this method will manage common session between app and webview/browser.

Leave a comment