Copie o código do código da seguinte forma:
<%@ page idioma="java" pageEncoding="UTF-8"%>
<html>
<cabeça>
<roteiro>
carga de função (cid)
{
var xmlhttp;
se (janela.XMLHttpRequest)
{// código para IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
outro
{// código para IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","date.jsp?cid="+cid,false);
xmlhttp.send();
var obj = eval("("+ xmlhttp.responseText+")");
//var obj=JSON.parse(xmlhttp.responseText); //IE8 e superior;
document.getElementById("teste").innerHTML=obj.weatherinfo.city+":"+obj.weatherinfo.weather1+" "+obj.weatherinfo.temp1;
}
</script>
</head>
<corpo>
<p id="test">Condições climáticas</p>
<button id="btn1" onClick=load("101280601")>Tempo em Shenzhen</button>
<button id="btn2" onClick=load("101250501")>Tempo em Chenzhou</button>
<!--
Aquisição de ID da cidade: http://blog.csdn.net/zgyulongfei/article/details/7956118
-->
</body>
</html>
data.jsp
Copie o código do código da seguinte forma:
<%@page idioma="java" import="java.net.*,java.io.*" pageEncoding="utf-8"%>
<%
String cid = request.getParameter("cid");
URL url = novo URL("http://m.weather.com.cn/data/"+cid+".html");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.connect();
InputStream cin = httpConn.getInputStream();
Leitor BufferedReader = new BufferedReader(new InputStreamReader(cin,"UTF-8"));
StringBuffer sb = new StringBuffer();
String rl = nulo;
while ((rl = leitor.readLine()) != nulo)
sb.append(rl);
out.println(sb);
%>