【发布时间】:2012-09-07 12:27:51
【问题描述】:
我正在为一个网站开发一个安卓应用程序。该网站支持 Disqus 的评论服务。我想在我的应用程序中支持相同的功能。我从 Disqus 文档中获得了 API,但我仍然不清楚如何将它们集成到我的应用程序中。请帮助我了解实施。是否有人将 Disqus 评论服务集成到他们的 Android 应用中?
【问题讨论】:
我正在为一个网站开发一个安卓应用程序。该网站支持 Disqus 的评论服务。我想在我的应用程序中支持相同的功能。我从 Disqus 文档中获得了 API,但我仍然不清楚如何将它们集成到我的应用程序中。请帮助我了解实施。是否有人将 Disqus 评论服务集成到他们的 Android 应用中?
【问题讨论】:
在将我的网站 disqus 线程与我的 android 应用程序链接时遇到了同样的问题。我写了一个小演练,如果您有兴趣,我将链接到下面的演练。基本上,在您的 android 应用程序中,您想使用 WebView 并使用可以获取您的 disqus 标识符的单独 php 文件。
【讨论】:
感谢 ndgreen。看到你的想法,我在没有 PHP 文件的情况下创建了一个不同的东西: https://gist.github.com/bichotll/5563926
这个脚本只是从一个简单的函数创建 html 并加载它。
【讨论】:
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL data:text/html;null,%3Cdiv%20id='disqus_thread'%3E%3C/div%3E%3Cscript%20type='text/javascript'%3Evar%20disqus_identifier%20=%20'null';var%20disqus_shortname%20=%20'jebalog';%20(function()%20{%20var%20dsq%20=%20document.createElement('script');%20dsq.type%20=%20'text/javascript';%20dsq.async%20=%20true;dsq.src%20=%20'http://'%20+%20disqus_shortname%20+%20'.disqus.com/embed.js';(document.getElementsByTagName('head')[0]%20||%20do ...
您可以使用以下代码: 谷歌登录正在工作。 我还没有测试过 Facebook。
static void setupDisqus(Context context, WebView disqus) {
try {
String URL = ""; // URL must be unique like identifier! Because Disqus, is using the url instead of identifier.
String identifier = "";
String shortName = "";
String commentsUri = "https://captainsp.github.io/disqus_comments_dark_gray.html?" + "shortname=" + shortName +
"&url=" + URLEncoder.encode(URL, "UTF-8") +
"&title=" + URLEncoder.encode("Comments", "UTF-8") +
"&identifier=" + URLEncoder.encode(identifier, "UTF-8");
/*
* You can use this colors in my Github Account:
* disqus_comments_dark_gray.html
* disqus_comments.html
* disqus_comments_dark.html
*
*
*/
disqus.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
new Handler().postDelayed(disqus::reload, 2000); // Reload Comments
super.onReceivedError(view, request, error);
}
});
CookieManager.getInstance().setAcceptThirdPartyCookies(disqus, true); // Accept Cookies to login (If you forget this part users need to login every single time)
disqus.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // Google / Facebook Login
disqus.getSettings().setSupportMultipleWindows(true); // Google / Facebook Login
CookieManager.getInstance().setAcceptCookie(true); // Accept Cookies to login 2
disqus.setWebChromeClient(new WebChromeClient() {
@SuppressLint("SetJavaScriptEnabled")
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView newWebView = new WebView(context); // Create new WebView
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString(webSettings.getUserAgentString().replace("; wv", "")); // Hide WebView User Agent
final Dialog dialog = new Dialog(context); // Create Dialog
dialog.setContentView(newWebView);
dialog.show();
CookieManager.getInstance().acceptThirdPartyCookies(newWebView);
newWebView.setWebViewClient(new WebViewClient());
newWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onCloseWindow(WebView window) {
dialog.dismiss(); // Close the dialog after logged in
}
});
((WebView.WebViewTransport) resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
return true;
}
});
disqus.getSettings().setJavaScriptEnabled(true); // Enable JavaScript
disqus.getSettings().setAppCacheEnabled(true);
disqus.getSettings().setDomStorageEnabled(true);
disqus.loadUrl(commentsUri);
} catch (Exception e) {
e.printStackTrace();
}
}
首先新建一个WebView或者用findViewById(R.id.webView);找到
然后将 Disqus 设置到您的 WebView:setupDisqus(this,webView);
更多信息:https://help.disqus.com/en/articles/1717165-javascript-embed-in-native-apps
【讨论】: