一、解决思路
1、首先获取泸州的城市代码
在返回的数据里,第一项的ref值就是泸州的城市代码:101271001
2、利用国家气象局提供的接口访问泸州的天气信息
二、准备工作
学会如何在Java程序里解析json。
准备jar包
连接:链接: 点击这里 提取码: 3nqw
1、创建Java项目JsonDemo
ParseJsonString
package net.qing.json;
import org.json.JSONObject;
public class ParseJsonString {
public static void main(String[] args) {
String strJson = "{'id':1,'name':'qing','age':100}";
try {
JSONObject jsonObject = new JSONObject(strJson);
System.out.println(jsonObject);
System.out.println("id:"+jsonObject.get("id"));
System.out.println("name:"+jsonObject.get("name"));
System.out.println("age:"+jsonObject.get("age"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
ParseJsonString02
package net.qing.json;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ParseJsonString02 {
public static void main(String[] args) {
String strJsonArr = "[{'id':1,'name':'qing','age':100}," +
"{'id':2,'name':'peng','age':10},"+
"{'id':3,'name':'lzb','age':190}]";
try {
JSONArray jsonArray = new JSONArray(strJsonArr);
for (int i = 0; i<jsonArray.length();i++){
System.out.println(jsonArray.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
三、功能实现
1、创建web项目并准备jar包
2、修改项目首页文件index.jsp
3、在web目录里创建查询天气页面query_weather.jsp
4、在web目录里创建处理查询天气页面do_query_weather.jsp
(1)显示表单提交的城市名数据
3)编写通过城市名查询天气信息的方法
<%!
……
public HashMap<String, String> getWeatherInfo(String cityName) throws JSONException {
HashMap<String, String> weather = new HashMap<>();
try {
String cityCode = getCityCode(cityName);
String strUrl = "http://t.weather.sojson.com/api/weather/city/" + cityCode;
URL url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
byte[] buffer = new byte[is.available()];
is.read(buffer);
String response = new String(buffer, "utf-8");
JSONObject weatherInfoJsonObj = new JSONObject(response);
System.out.println(weatherInfoJsonObj);
JSONObject cityInfoJsonObj = new JSONObject(weatherInfoJsonObj.getString("cityInfo"));
weather.put("城市:", cityInfoJsonObj.getString("parent") + " " + cityInfoJsonObj.getString("city") + "[" + cityCode + "]");
JSONArray forcastJsonArr = new JSONArray(new JSONObject(weatherInfoJsonObj.getString("data")).getString("forecast"));
weather.put("时间:", weatherInfoJsonObj.getString("time") + " " + forcastJsonArr.getJSONObject(0).getString("week"));
weather.put("温度:", forcastJsonArr.getJSONObject(0).getString("high") + " " + forcastJsonArr.getJSONObject(0).getString("low"));
weather.put("天气:", forcastJsonArr.getJSONObject(0).getString("type"));
} else {
weather.put("错误:", "[" + cityName + "]不存在!");
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
%>
do_query_weather.jsp页面完整代码
<%@ page import="java.net.URL" %>
<%@ page import="java.net.HttpURLConnection" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="org.json.JSONArray" %>
<%@ page import="org.json.JSONException" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>显示城市天气信息</title>
</head>
<body>
<%!
public String getCityCode(String cityName) {
String cityCode = "";
try {
String strUrl = "http://toy1.weather.com.cn/search?cityname=" + URLEncoder.encode(cityName, "utf-8");
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
byte[] buffer = new byte[is.available()];
if (is.available() > 4) {
is.read(buffer);
String response = new String(buffer, "utf-8");
cityCode = response.substring(10, 19);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return cityCode;
}
public HashMap<String, String> getWeatherInfo(String cityName) throws JSONException {
HashMap<String, String> weather = new HashMap<>();
try {
String cityCode = getCityCode(cityName);
String strUrl = "http://t.weather.sojson.com/api/weather/city/" + cityCode;
URL url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
byte[] buffer = new byte[is.available()];
is.read(buffer);
String response = new String(buffer, "utf-8");
JSONObject weatherInfoJsonObj = new JSONObject(response);
System.out.println(weatherInfoJsonObj);
JSONObject cityInfoJsonObj = new JSONObject(weatherInfoJsonObj.getString("cityInfo"));
weather.put("城市:", cityInfoJsonObj.getString("parent") + " " + cityInfoJsonObj.getString("city") + "[" + cityCode + "]");
JSONArray forcastJsonArr = new JSONArray(new JSONObject(weatherInfoJsonObj.getString("data")).getString("forecast"));
weather.put("时间:", weatherInfoJsonObj.getString("time") + " " + forcastJsonArr.getJSONObject(0).getString("week"));
weather.put("温度:", forcastJsonArr.getJSONObject(0).getString("high") + " " + forcastJsonArr.getJSONObject(0).getString("low"));
weather.put("天气:", forcastJsonArr.getJSONObject(0).getString("type"));
} else {
weather.put("错误:", "[" + cityName + "]不存在!");
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
%>
<%
request.setCharacterEncoding("utf-8");
String cityName = request.getParameter("city_name");
HashMap<String, String> weather = getWeatherInfo(cityName);
for (String key: weather.keySet()) {
out.print(key + weather.get(key) + "<br/>");
}
%>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)