本文介绍了在android系统4.4+之间的HttpClient和web视图问题分享会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分享HTTP客户端和网络之间的会话查看它工作正常,我的是Android 4.1或更早的版本,但它不是在4.4或以上版本的工作吗?我不能够找出reason.any帮助将大大AP preciated
我的code是

 公共类的AppSettings扩展应用
{
    私有静态最终DefaultHttpClient客户端= createClient();    @覆盖
    公共无效的onCreate()
    {
    }    公共静态DefaultHttpClient getClient()
    {
        返回客户端;
    }    私有静态DefaultHttpClient createClient()
    {
        BasicHttpParams PARAMS =新BasicHttpParams();
        SchemeRegistry schemeRegistry =新SchemeRegistry();
        schemeRegistry.register(新计划(HTTP,PlainSocketFactory.getSocketFactory(),80));
        最终的SSLSocketFactory的SSLSocketFactory = SSLSocketFactory.getSocketFactory();
        schemeRegistry.register(新计划(https开头,SSLSocketFactory的,4​​43));
        ClientConnectionManager厘米=新ThreadSafeClientConnManager(参数,可以schemeRegistry);
        DefaultHttpClient的HttpClient =新DefaultHttpClient(厘米,则params);
        httpclient.getCookieStore()的getCookies()。
        返回HttpClient的;
    }
}

虽然做的http请求我使用下面code

 尝试
        {            DefaultHttpClient mClient = AppSettings.getClient();
            HttpPost httppost =新HttpPost(URL);            httppost.setEntity(新UrlEn codedFormEntity(的NameValuePair));            //httppost.setEntity(responseBody);            HTT presponse响应= mClient.execute(httppost);            如果(响应!= NULL)
            {
                responseBody = EntityUtils.toString(response.getEntity());            }
            其他
            {
                CustomLogger.showLog(测试版,响应为空);
            }        }    赶上(例外五)
    {
        CustomLogger.showLog(测试版,异常在发送数据);
        e.printStackTrace();
    }

为了与web视图我使用下列共享code

  webView.setWebViewClient(新WebViewClient()
                    {                        @覆盖
                        公共无效onPageStarted(的WebView视图,字符串URL,位图图标)
                        {
                            super.onPageStarted(查看,网址,NULL);
                            CustomLogger.showLog(测试版,页当前已启动的网址为+网址);
                            webView.setVisibility(View.GONE);
                            progress_loader.setVisibility(View.VISIBLE);
                            DefaultHttpClient mClient = AppSettings.getClient();
                            饼干sessionInfo;
                            清单< Cookie和GT;饼干= mClient.getCookieStore()的getCookies()。
                            如果(!cookies.isEmpty())
                            {
                                CookieSyncManager.createInstance(getActivity());
                                CookieManager cookieManager = CookieManager.getInstance();                                对于(饼干饼干:饼干)
                                {
                                    sessionInfo =饼干;
                                    串cookieString = sessionInfo.getName()+=+ sessionInfo.getValue()
                                            +;域=+ sessionInfo.getDomain();
                                    CustomLogger.showLog(测试版,cookie字符串为+ cookieString);
                                    cookieManager.setCookie(http://www.example.com,cookieString);
                                    CookieSyncManager.getInstance()同步()。
                                }
                            }
                            其他
                            {
                                CustomLogger.showLog(测试版,无曲奇);                            }
                        }
}


解决方案

我已经解决了我的问题。 Web视图OnPageStarted事件获取调用两次,我被检索的cookie,请参阅本链接。随着简单的计数器,我仅限于第一次,现在它工作正常。

cookie的检索

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");

                            }
                        }
}
解决方案

I have solved my problem. OnPageStarted event of web view was getting called twice in which i was retrieving cookies,refer this link.With simple counter ,i restricted the retrieval of cookie only on first occasion and now its working fine

这篇关于在android系统4.4+之间的HttpClient和web视图问题分享会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:05