我在以下链接的帮助下创建了一个应用程序但是当我登录Disqus以进行评论时,我需要刷新页面,然后它才显示我已登录然后我可以输入评论。我使用的是HTML代码段而不是php代码段。 我所指的链接是: -
http://globeotter.com/blog/disqus-android-code/
http://globeotter.com/blog/disqus-login-and-logout/
我的代码如下: -
Public class MainActivity extends Activity {
WebView webDisqus;
String sMyurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String htmlComments = getHtmlComment("<MyWEBSITE_Disqus_ID>", "<MYWEBSITE_SHORTNAME>");
sMyurl="MYWEBSITES_URL";
webDisqus = (WebView) findViewById(R.id.disqus);
// set up disqus
WebSettings webSettings2 = webDisqus.getSettings();
webSettings2.setJavaScriptEnabled(true);
webSettings2.setBuiltInZoomControls(true);
webDisqus.requestFocusFromTouch();
webDisqus.setWebViewClient(new MyWebViewClient(htmlComments, "text/html", null, sMyurl));
webDisqus.setWebChromeClient(new WebChromeClient());
webDisqus.loadData(htmlComments, "text/html", null);
}
public String getHtmlComment(String idPost, String shortName) {
return "<div id='disqus_thread'></div>"
+ "<script type='text/javascript'>"
+ "var disqus_identifier = '"
+ idPost
+ "';"
+ "var disqus_shortname = '"
+ shortName
+ "';"
+ " (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;"
+ "dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';"
+ "(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();"
+ "</script>";
}
}
MyWebView类如下: -
`
public class MyWebViewClient extends WebViewClient {`
private String html_comments, sType, obj, sMyURL;
public MyWebViewClient(String htmlComments, String string, String object, String URL) {
// TODO Auto-generated constructor stub
html_comments=htmlComments;
sType=string;
obj=object;
sMyURL=URL;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("page started", url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("disqus error", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
}
public void onPageFinished(WebView view, String url) {
if(url.indexOf("logout")>-1 || url.indexOf("disqus.com/next/login-success")>-1 ){
view.loadData(html_comments, sType, obj);
}
if(url.indexOf("disqus.com/_ax/twitter/complete")>-1||url.indexOf("disqus.com/_ax/facebook/complete")>-1||url.indexOf("disqus.com/_ax/google/complete")>-1){
view.loadData(html_comments, sType, obj);
}
if(url.indexOf(sMyURL+"/login.php")>-1){
view.loadData(html_comments,sType, obj);
}
}
}`
任何人都可以告诉我,当我从Disqus通过我的活动的WebView登录和注销时,我该怎么做才能刷新页面。
答案 0 :(得分:0)
以上代码在Android设备上运行良好。如果我们在模拟器上运行它,只会出现问题,因为与设备相比,模拟器速度很慢......