我的应用程序包含一个包含Web视图的片段。当我尝试使用其他网址时,网页视图功能正常,但是当我尝试使用" http://peekatu.com/scrapper/onair.php"它不会在Web视图中加载。我不知道为什么它不会加载。感谢帮助。谢谢。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v= inflater.inflate(R.layout.fragment_listen_live, container, false);
btn = (Button) v.findViewById(R.id.button1);
btn.setOnClickListener(pausePlay);
web_v= (WebView) v.findViewById(R.id.webView);
WebSettings web_sett=web_v.getSettings();
web_sett.setJavaScriptEnabled(true);
//web_v.setWebViewClient(new MyWebClient());
web_v.setWebChromeClient(new WebChromeClient());
web_v.loadUrl("http://peekatu.com/scrapper/onair.php");
MainActivity m = (MainActivity) getActivity();
m.changeMenu("homePage");
this.getSherlockActivity().getSupportActionBar().setTitle("Power 92 Chicago");
this.getSherlockActivity().getSupportActionBar()
.setSubtitle("#1 in the Streets!");
return v;
}
private class MyWebClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
web_v.loadUrl(url);
return true;
}
}
onair.php
<?php
set_time_limit(0);
ignore_user_abort(FALSE);
error_reporting(E_ERROR);
ini_set('display_errors', 'on');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.power92chicago.com/');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
$dom = new DOMDocument();
$dom->loadHTML(curl_exec($ch));
$xpath = new DOMXPath($dom);
$node = $xpath->query("//td[contains(concat(' ', normalize-space(@class), ' '), ' TimeFeatureHorizontalTd ')]")->item(0);
echo get_inner_html($node);
function get_inner_html($node) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
return $innerHTML;
}
?>
答案 0 :(得分:0)
在致电loadUrl
之前,请添加以下一行:
web_v.setWebChromeClient(new WebChromeClient());
另外,我不知道为什么你定义WebViewClient
覆盖shouldOverrideUrlLoading
,然后自己调用loadUrl
- 只需删除它。
答案 1 :(得分:0)
使用移动浏览器测试网页无法加载,因此问题肯定不在您的Android代码中,问题出在php代码上。
您可以共享代码来生成该网络吗?
此外,如果您要尝试在应用中显示信息,则可能需要考虑不同的方法,如果要在应用中显示数据列表,则可能需要使用listView并获取数据来自JSON REST Api,而不是在php网站上显示该列表