0

I am loading an URL in webview. i.e. first I am loading a login page in a Webview. Then after user type username and password and tap login button in webview page, it shows the particular session key for that logon in the webview. Now I want to read that session key and hide that information from user. Is anybody has the idea how to do that?

Thanks.

creative Pro
  • 57
  • 1
  • 7
  • I'm sure lots of people have lots of different ideas on how to do that. Lets start with what you have tried. Show us all your code and any errors or problems you are having with it. – Tigger Mar 28 '16 at 09:57
  • http://stackoverflow.com/questions/10213487/how-can-i-get-the-json-response-of-a-post-request-in-a-webview save key in shared preference for further use. – ashish Mar 28 '16 at 10:03
  • Basically I just want to read the session key. Now it is displaying in the webview body. I couldn't figure out how to read that value. Once we read, we can redirect it to another web page – creative Pro Mar 28 '16 at 10:13

2 Answers2

0

The same question has been answered multiple times. And all the answers refer to the this post. Though this post has warnings in the end, I could not find any other eaiser way to achive what tou are looking for. Below is the copy-paste of the code from this post.

final Context myApp = this;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
    @JavascriptInterface
    @SuppressWarnings("unused")
    public void processHTML(String html)
    {
        // process the html as needed by the app
    }
}

    //Edit 1 start
 final ProgressDialog pd = ProgressDialog.show(OnlinePaymentActivity.this, "", "Please wait, your transaction is being processed...", true);

//Edit 1 end

final WebView browser = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
browser.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called HTMLOUT */
browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

/* WebViewClient must be set BEFORE calling loadUrl! */
browser.setWebViewClient(new WebViewClient() {

/////Edit 2 start
                  @Override
                  public void onPageStarted(WebView view, String url, Bitmap favicon)
                  {


            If(browser.getVisibility() == View.VISIBLE)
{
                      browser.setVisibility(View.GONE);
}
                      pd.show();
                  }
///// Edit 2 end

    @Override
    public void onPageFinished(WebView view, String url)
    {

//Edit 3 start

 If(browser.getVisibility() == View.GONE)
{
                      browser.setVisibility(View.VISIBLE);
}

     pd.dismiss();
// Edit 3 end

        /* This call inject JavaScript into the page which just finished loading. */
        browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    }
});

/* load a web page */
browser.loadUrl("http://lexandera.com/files/jsexamples/gethtml.html");
Rishabh Lashkari
  • 638
  • 1
  • 8
  • 22
  • Hi, I am able to read the html content. Now I want to stop showing that web page to the end user. Basically I need to read the token which is displayed in the webview and I don't want to show that to the end user. So how should I stop loading that? When it comes to onPageFinished(), it is already shown to the user. – creative Pro Mar 29 '16 at 08:28
  • Though I don't know if this would be the perferct solution but, i would suggest you to try this. Use setVisiblity for the webview, when the webpage is being loaded you can hide the webview and when you have loaded your next page you can show the webview again. In between you can show a progressDialog for the user to wait for a while. – Rishabh Lashkari Mar 29 '16 at 11:04
0

I found a way of doing this. i.e. First I am reading the token in the body text and once the token found, make the webView to invisible. Your suggestion is not applicable for me as sometimes I want to show the login page. i.e. if the user is not login to the particular account, we should show the web view. Once they return the token after login, webview should be hidden.

creative Pro
  • 57
  • 1
  • 7