4

我使用下面的代码来显示我的 Twitter 登录页面。

第一次,它正在调用public void onPageStarted----->public void onPageFinished(WebView view, String url),在我登录后它正在重定向 url 和调用public boolean shouldOverrideUrlLoading()。所以流程看起来像:

  1. onPageStarted
  2. onPageFinished
  3. shouldOverrideUrlLoading

但是当我注销并第二次来时,我期望与上面相同的流程,但流程更改为:

  1. onPageStarted
  2. shouldOverrideUrlLoading
  3. onPageFinished

我不明白为什么会发生这种变化,以及为什么shouldOverrideUrlLoading()即使之前没有 URL 也会调用它。

我打印了 webviews url,null但这仍然是它重定向到此方法的原因。

    private class TwitterWebViewClient extends WebViewClient {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                Log.d(TAG, "Redirecting URL " + url);

                if (url.startsWith(TwitterApp.CALLBACK_URL)) {
                    mListener.onComplete(url);
                    Log.e("starts","starts with");
                    TwitterDialog.this.dismiss();

                    return true;
                }  else if (url.startsWith("authorize")) {

                    Log.e("authorize","authorization");
                    return false;
                }

                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.d(TAG, "Page error: " + description);

                super.onReceivedError(view, errorCode, description, failingUrl);

                mListener.onError(description);

                TwitterDialog.this.dismiss();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("page started in",url);
                Log.d(TAG, "Loading URL: " + url);
                super.onPageStarted(view, url, favicon);
                mSpinner.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                String title = mWebView.getTitle();
                if (title != null && title.length() > 0) {
                    Log.e("title","settitle");
                    mTitle.setText(title);
                }
                mSpinner.dismiss();
            }

        }
4

1 回答 1

-1

在 Android < 3.0 上的 shouldOverrideUrlLoading 上有一个错误,我有这段代码来获取 twitter oauth:

authorizationWebView.setWebViewClient(new WebViewClient() {

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);

                    // 11 = Build.VERSION_CODES.HONEYCOMB (Android 3.0)
                    if (Build.VERSION.SDK_INT < 11) {

                        // According to this page:
                        //
                        // http://www.catchingtales.com/android-webview-shouldoverrideurlloading-and-redirect/416/
                        //
                        // shouldOverrideUrlLoading() is not called for redirects on
                        // Android earlier than 3.0, so call the method manually.
                        //
                        // The implementation of shouldOverrideUrlLoading() returns
                        // true only when the URL starts with the callback URL and
                        // dummyCallbackUrl is true.

                        if (shouldOverrideUrlLoading(view, url)) {
                            view.stopLoading();
                        }
                    }
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    // If the url is the same host, not override (api.twitter.com)
                    if (Uri.parse(url).getHost().equals(Uri.parse(Constants.AUTHORIZE_URL).getHost())) {

                        return false;

                    } else if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) {

                        // Retrieve access token in vase of oauth_verifier
                        Uri uri = Uri.parse(url);
                        String verifier = uri.getQueryParameter("oauth_verifier");

                        twitterOauth.retrieveAccessToken(verifier, MainActivity.this);

                        return true;

                    }

                    // If the user clicks in this webview on a link of other host, redirect to a browser.
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);

                    return true;

                }

            });

希望这段代码对您有所帮助。

于 2013-02-12T10:59:51.840 回答