Android WebView Keep Login issues

Source: Internet
Author: User

A recent requirement is this: Add a mall to the app, and the implementation of the mall is H5 (including login). You need to embed this H5 into your native app and add payment functionality to your native code.

When received this demand, think this is not very simple, with webview load this page, through the JS callback native method, then call third party payment, completed.

But when it comes to real development, the problem arises.

The first problem is:

1. In the Mall, click on a product, then confirm the purchase.

2. No login at this time, will jump to the login screen.

3. It doesn't matter, just log in.

4. Once you are done, click on this item again and then go to buy it.

5. Then the problem arises, is not the previous login? Why did you jump to the login screen?

Analysis:

At first thought is H5 writes the wrong, however I use the browser to open, is good, jumps also normal, therefore, is still webview the problem which sets.

After a pass Baidu, preliminary determination is the problem of cookie synchronization, but the network this piece has been not very understand, the present also did not have time to study carefully, so follow Baidu obtained step by step to try. Here is my workaround.

Workaround:

    • Initialize WebView
Private voidInitwebview () {websettings websettings=webview.getsettings (); //Set EncodingWebsettings.setdefaulttextencodingname ("Utf-8"); Websettings.settextzoom (100); //self-adapting screenWebsettings.setlayoutalgorithm (WebSettings.LayoutAlgorithm.SINGLE_COLUMN); Websettings.setloadwithoverviewmode (true); //Support JSWebsettings.setjavascriptenabled (true); //set local call objects and their interfacesJsbridge =NewJavascriptbridge (Getactivity (), webview); Webview.addjavascriptinterface (Jsbridge,"Native"); Cookiemanager Mcookiemanager=cookiemanager.getinstance (); Mcookiemanager.setacceptcookie (true); Mcookiemanager.setacceptthirdpartycookies (WebView,true); Webview.setwebviewclient (Newwebviewclient () {@Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {Super. onpagestarted (view, URL, favicon); } @Override Public voidonpagefinished (WebView view, String URL) {// //Get cookie information inside the WebViewCookiemanager Cookiemanager =cookiemanager.getinstance (); Cookiemanager.setacceptcookie (true); String cookiestr=cookiemanager.getcookie (URL);                Savecookies (COOKIESTR);            Progressbar.setvisibility (View.gone); } @Override Public voidOnreceivederror (WebView view,intErrorCode, String description,FinalString Failingurl) {            }             Public Booleanshouldoverrideurlloading (WebView view, String URL) {if(Url.startswith ("http") | | url.startswith ("https")) {                    if(Url.startswith ("HTTP://SHOP.YINM")) {//this webview is generic and only makes a cookie operation on the link to the mall. Synccookie (GetContext (), URL);                    } view.loadurl (URL); return false; } Else {                    //the other URLs will open a acitity and then call the native app.                    Try{Intent in=NewIntent (Intent.action_view, Uri.parse (URL)); In.setflags (Intent.flag_activity_new_task|intent.flag_activity_single_top);                        StartActivity (in);                    View.stoploading (); }Catch(Exception e) {e.printstacktrace (); }                    return true;    }            }        }); }
View Code

    • When onpagefinished, save the cookie
    Private void savecookies (String cookiestring) {        sp.edit (). Putstring ("Cook", cookiestring). Apply ();    
View Code

    • Before Loadurl (URL), sync cookies
    Private voidSynccookie (context context, String URL) {Try{cookiesyncmanager.createinstance (context); Cookiemanager Cookiemanager=cookiemanager.getinstance (); Cookiemanager.setacceptcookie (true); Cookiemanager.removesessioncookie ();//removed from//LogString Oldcookie =cookiemanager.getcookie (URL); if(Oldcookie! =NULL) {android.util.log.i ("Zxy", "GetCookie Oldcookie" +Oldcookie); } String Cookie= Sp.getstring ("Cook", "" "); String[] Cookies= Cookie.split (";"); if(!textutils.isempty (Cookie)) {                 for(String cook:cookies) {cookiemanager.setcookie (URL, Cook.trim ());//must be a set, if it is stitching, the settings are unsuccessful.                 }            }            //Finally, be sure to callcookiesyncmanager.getinstance (). sync (); //LogString Newcookie =cookiemanager.getcookie (URL); if(Newcookie! =NULL) {android.util.log.i ("Zxy", "GetCookie Newcookie" +Newcookie); }        }Catch(Exception e) {android.util.log.i ("Zxy", "GetCookie failed" +e.tostring ()); }    }
View Code

This enables the cookie to be synchronized.

(Network this piece, find a time to be good to fill up)

Android WebView Keep Login issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.