我使用以下代码显示我的Twitter登录页面。
这是第一次调用public void onPageStarted----->public void onPageFinished(WebView view, String url)
,我登录后重定向网址并调用public boolean shouldOverrideUrlLoading()
。所以流程看起来像:
onPageStarted
onPageFinished
shouldOverrideUrlLoading
但是当我退出并第二次来时,我期望像上面这样的流程,但流程改为:
onPageStarted
shouldOverrideUrlLoading
onPageFinished
我不明白为什么这会改变,以及为什么它会调用shouldOverrideUrlLoading()
,即使之前没有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();
}
}
答案 0 :(得分:-1)
Android上的shouldOverrideUrlLoading存在错误< 3.0,我有这个代码来获取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;
}
});
希望此代码可以帮助您。