1

好吧,我正在尝试在外部服务器上授权我的 android 应用程序,首先我加载用户输入其用户名和密码的 URL,然后该页面将我重定向到我的页面上并向我发送我应该获取的“代码”和“状态”授权令牌。我正在尝试使用检索该代码,JavascriptInterface但不幸的是没有收到任何东西......所以有一个问题,为什么我没有得到代码或状态?

一些编码:

我的安卓端:

@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();
            }
        }
    }

网页端:

<?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>
        ";
        }
    ?>

注意:此页面上有一些打印 html,所以我得到了代码和状态,但是函数 sendToAndroid 不能正常工作,或者我的 Java 中的某些东西......这两个if条件都是正确的。

日志输出:

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
4

0 回答 0