android如何在android中传递url?
我正在尝试从服务器动态传递url,所以我要做的是
if(ResponseProduct.video!=null) {
String html = "<iframe width=\"450\" height=\"260\" src=\""+ResponseProduct.video+"\" ></iframe>";
WebView webView = (WebView) view.findViewById(R.id.video);
webView.setVisibility(View.VISIBLE);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setHorizontalScrollBarEnabled(false);
webView.loadData(html, "text/html; video/mpeg", "UTF-8");
}
最佳答案
您需要将String
url路径放入src
中,例如:
String videoPath="http://api.thingspeak.com/channels/31592/charts/1?width=450&height=260&results=60&dynamic=true\\";
String html = "<iframe width=\"450\" height=\"260\" style=\"border: 1px solid #cccccc;\" src=\""+videoPath+" ></iframe>";
如果videourl为空或无效,则页面未加载,则需要隐藏webview
代码如下:
private boolean isValidUrl(String url) {
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(url.toLowerCase());
if(m.matches())
return true;
else
return false;
}
然后加载到
webView
webview.getSettings().setJavaScriptEnabled(true);
if(videoPath!=null && isValidUrl(videoPath)
{
mWebView.setVisibility(View.VISIBLE);
webview.loadData(html, "text/html", null);
}
else
webview.setVisibility(View.GONE);