我正在尝试将一个推特小部件嵌入到webview中,但感觉JS代码没有执行。所有显示的都是“Twitter @xxxxxx”
WebView webView = new WebView(getActivity());
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_LONG).show();
}
});
String summary = "<a class=\"twitter-timeline\" href=\"https://twitter.com/"+getArguments().getString("handle")+"\" data-widget-id=\""+getArguments().getString("widgetId")+"\">Tweets by @"+getArguments().getString("handle")+"</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>";
webView.loadData(summary, "text/html", null);
return webView;
我的应用程序确实具有Intenet访问权限
答案 0 :(得分:7)
尝试使用webView.loadDataWithBaseUrl并将基本网址设置为https://twitter.com
答案 1 :(得分:0)
我创建了动态嵌入Twitter webview ..
从GooglesGuy获取想法......这对任何人都有帮助..
private void addWebViewTwitter(Object twitterContent){
if(twitterContent!=null && twitterContent.toString().length()>0){
WebView webView=new WebView(this);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setPluginsEnabled(true);
if(Utils.isNetworkAvailable(this)) {
webView.loadDataWithBaseURL("https://twitter.com",twitterContent.toString(), "text/html", "utf-8","");
}
}
}
twitterContent - 您的嵌入twitterhtml内容