I sharing session between http client and web view it works fine for me in android 4.1 or earlier versions but its not working in 4.4 or above versions? i am not able to figure out the reason.any help will be greatly appreciated My code is
public class AppSettings extends Application
{
private static final DefaultHttpClient client = createClient();
@Override
public void onCreate()
{
}
public static DefaultHttpClient getClient()
{
return client;
}
private static DefaultHttpClient createClient()
{
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
httpclient.getCookieStore().getCookies();
return httpclient;
}
}
While making http request i am using below code
try
{
DefaultHttpClient mClient = AppSettings.getClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
//httppost.setEntity(responseBody);
HttpResponse response = mClient.execute(httppost);
if (response != null)
{
responseBody = EntityUtils.toString(response.getEntity());
}
else
{
CustomLogger.showLog("Beta", "Response is null");
}
}
catch (Exception e)
{
CustomLogger.showLog("Beta", "Exception in Sending data");
e.printStackTrace();
}
in order to share it with webView i am using following code
webView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
super.onPageStarted(view, url, null);
CustomLogger.showLog("Beta", "onPage Started url is" + url);
webView.setVisibility(View.GONE);
progress_loader.setVisibility(View.VISIBLE);
DefaultHttpClient mClient = AppSettings.getClient();
Cookie sessionInfo;
List<Cookie> cookies = mClient.getCookieStore().getCookies();
if (!cookies.isEmpty())
{
CookieSyncManager.createInstance(getActivity());
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : cookies)
{
sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue()
+ "; domain=" + sessionInfo.getDomain();
CustomLogger.showLog("Beta", "cookie string is " + cookieString);
cookieManager.setCookie("http://www.example.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}
else
{
CustomLogger.showLog("Beta", "No cookies");
}
}
}