WebViewClient redirects even though URL is missing

I used the code below to show my twitter login page.

For the first time, it calls public void onPageStarted----->public void onPageFinished(WebView view, String url) , and after logging in, it redirects the URL and calls public boolean shouldOverrideUrlLoading() . So the stream looks like this:

  • onPageStarted
  • onPageFinished
  • shouldOverrideUrlLoading

But when I go to the second exit, I expect the same thread as above, but the thread has changed to:

  • onPageStarted
  • shouldOverrideUrlLoading
  • onPageFinished

I did not understand why this has changed and why it calls shouldOverrideUrlLoading() , even if the URL is missing.

I printed the url webviews, it is null , but still why does it redirect this method.

  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
source share
1 answer

There is an error on shouldOverrideUrlLoading on Android <3.0, I have this code to get 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; } }); 

We hope this code helps you.

-1
source

All Articles