1

well I am trying to authorize my android app on extern server, first I load URL on which user enters its username and password, then that page redirects me on my page and sends me a "code" and "state" with which I should obtain authorization token. I am trying to retrieve that code using JavascriptInterface but unfortunately am not recieving anything... so there is an issue, why am I not geting code nor state?

some codez:

my android side:

@SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.web_view);

        web_view = (WebView) findViewById(R.id.web_view);
        web_view.getSettings().setJavaScriptEnabled(true);
        web_view.addJavascriptInterface(new IJavascriptHandler(this),
                "AndroidBridge");

        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        progressBar = ProgressDialog.show(LoginActivity.this,
                "Authentication", "Loading...");

        web_view.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {

                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                return;
                            }
                        });
                alertDialog.show();
            }
        });

        web_view.loadUrl(authorizationURL);
    }

    public class IJavascriptHandler {
        LoginActivity activity;

        public IJavascriptHandler(LoginActivity activity) {
            this.activity = activity;
        }

        @JavascriptInterface
        public void sendToAndroid(String code, String state) {
            // this is called from JS with passed value
            try {
                Log.v("LOGIN CODE + state", code + "   " + state);
            } catch (Exception e) {
                Log.v("LOGIN ERROR", ":(");
                e.printStackTrace();
            }
        }
    }

web side:

<?php
    if(isset($_GET['code'])){
        echo "<html>



            <body>
            <script type=\"text/javascript\">

                    if(window.AndroidBridge){
                    window.AndroidBridge.sendToAndroid(" .  $_GET["code"] . ", " . $_GET["state"] . ");
                    }
                else {
                    alert(\"Error\");
                    }

        </script>
            <p>Success!</p>
            <p>" . $_GET["code"] . "</p>
            <p>" . $_GET["state"] . "</p>
            </body>
        </html>
        ";
        }
    ?>

note: there is some printing html on this page, so I am getting both code and state, but function sendToAndroid is not working properly or maybe something in my Java ... both if conditions are true.

log output:

09-25 15:55:07.257: D/webview(28931): blockWebkitViewMessage= false
09-25 15:55:10.627: D/WebView(28931): loadUrlImpl: called
09-25 15:55:10.627: D/WebCore(28931): uiOverrideUrlLoading: shouldOverrideUrlLoading() returnstrue
09-25 15:55:10.627: D/webcore(28931):  CORE loadUrl: called
09-25 15:55:10.627: D/webkit(28931): Firewall not null
09-25 15:55:10.632: D/webkit(28931): euler: isUrlBlocked = false
Darko Rodic
  • 1,010
  • 3
  • 10
  • 27
  • Hi! Could you provide me with your testing device and SDK version? I faced with the similar problem and I'm looking for a solution. Did u find smth? http://stackoverflow.com/questions/19245555/calling-javascriptinterface-method-through-the-webview-and-js-function-after-cha – validcat Oct 08 '13 at 12:10
  • It can help u maybe. http://stackoverflow.com/questions/9446868/access-ui-from-javascript-on-android – Adrian Sep 20 '16 at 02:25

0 Answers0