用于 Facebook Like 按钮的 Android WebView [英] Android WebView for Facebook Like Button
问题描述
我正在尝试在 Android WebView 中创建类似 facebook 的功能(项目规范不允许打开浏览器,或任何应用程序外活动).
I'm trying to make facebook like functionality in Android WebView (project specification does not allow browser opening, or any out of application activity).
所以,限制是它必须在 WebView 中完成.我设法使它成为一个对话框,并且在用户单击按钮时,它(WebView)成功重定向(在同一视图中)到 facebooks 登录页面.身份验证成功后,WebView
(在对话框中)将重定向到带有 Facebook 标题的空白页面.
So, restrictions are that it has to be done in WebView. I've managed to make it a dialog, and apon user's click like button, it (the WebView) redirects successfully (in the same view) to facebooks login page. After successful authentication, the WebView
(in a dialog) is redirected to blank page with facebook header.
有趣的是,当用户离开空白对话框并再次单击喜欢"按钮时,它工作得非常完美(喜欢和不喜欢) - 它以某种方式保持身份验证处于活动状态.为了解决空白页,我尝试/使用了以下内容:
Interestingly enough, when user leaves the blank dialog and click again on the like button it works like perfectly (like and unlike) - it somehow keeps authentication active. To resolve the blank page, I've tried/used following:
- 使用
WebViewClient
和shouldOverloadUrlForwarding
将整个过程保持在同一个WebView
对话框中. - 使用
WebChromeClient
正确执行 JavaScript - 登录后没有它就不可能喜欢/不喜欢. - 尝试使用
setUserAgentString()
模拟其他浏览器,如 Chrome 或 Firefox 尝试了 SSL 错误证书处理(在 API 级别 8)(在
WebViewClient
)
- using
WebViewClient
andshouldOverloadUrlForwarding
to keep whole process in sameWebView
dialog. - using
WebChromeClient
to properly execute JavaScript - without it after login is not possible to like/unlike. - tried using
setUserAgentString()
to simulate other browsers like Chrome or Firefox tried the SSL Error certificate handling (in API level 8) (at
WebViewClient
)
@Overridepublic void onReceivedSslError(WebView 视图,SslErrorHandler 处理程序,SslError 错误){handler.proceed();}
使用(以及所有可能的组合)
using (and all possible combination of these)
webView.getSettings().setAppCacheEnabled(true);webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
还尝试使用 CookieSyncManager
、CookieManager
和手动处理来持久化 cookie.
Tried also persisting cookies with CookieSyncManager
, CookieManager
and manually handling.
这一切都没有结果.我非常感谢任何帮助!
All of this was with no result. I really appreciate any help!
推荐答案
要跳过空白页,您可以这样做:
To get past the blank page you do this:
webview.setWebViewClient(new LikeWebviewClient(this));
private class LikeWebviewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished url: " +url);
// Facebook redirects to this url once a user has logged in, this is a blank page so we override this
// http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
if(url.startsWith("http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php")){
String redirectUrl = getFacebookLikeUrl();
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
这篇关于用于 Facebook Like 按钮的 Android WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!