我有一个html
页面,我正在通过Ajax
每隔一段时间更新。我正在尝试在对话框中使用 Volley 在 android 应用程序中检索此页面数据。它不是加载html正文中所有可用的数据,而是检索静态数据。
1.html 文件。
<html>
<head>
<script>
var request;
window.onload = sendInfo;
function sendInfo()
{
var v=3;
var url="index.jsp?val="+v;
if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
try
{
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send(null);
setTimeout("sendInfo()", 1000); // loading same screen after 1 sec
}
catch(e)
{
alert("Unable to connect to server");
}
}
function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('data').innerHTML=val;
}
}
</script>
</head>
<body>
<p>this is static data</p>
<span id="data"> </span>
</body>
</html>
文件 2.jsp 文件。
<%
double n = Math.random();
out.print(n+"<br>");
%>
上面的代码在Tomcat服务器中运行,页面每秒刷新一次。
安卓部分 :
主活动.java
url is address of html mentioned above.
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Document doc = Jsoup.parse(response.toString());
// String parsedText =response.toString().substring(500);
String parsedText = doc.body().text();
pDialog.setMessage(parsedText);
// pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.setMessage(error.getMessage());
// pDialog.hide();
}
});
In emulator it is loading only static data of html file "this is static
data" . If I try to load complete response string, in that case value of span(commented above - // String parsedText =response.toString().substring(500);) is blank, though in browser debug mode I am able to see its value getting updated every second.
请指出我在这里做错的地方。如果我的方法错误,那么请提出更好的方法来实现它。
这是
错误字符串解析文本 = doc.body().text();正确String parsedText = doc.body().toString;