http://api.map.baidu.com/telematics/v3/weather?
{
"error": 0,
"status": "success",
"date": "2014-05-03",
"results": [
{
"currentCity": "北京",
"weather_data": [
{
"date": "周六(今天, 实时:22℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/leizhenyu.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
"weather": "雷阵雨转多云",
"wind": "北风5-6级",
"temperature": "24 ~ 11℃"
},
{
"date": "周日",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云转晴",
"wind": "北风4-5级",
"temperature": "19 ~ 8℃"
},
{
"date": "周一",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微风",
"temperature": "21 ~ 9℃"
},
{
"date": "周二",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "多云转晴",
"wind": "微风",
"temperature": "21 ~ 10℃"
}
]
}
]
}
xml格式:
<CityWeatherResponse>
<status>successstatus>
<date>2014-05-03date>
<results>
<currentCity>北京currentCity>
<weather_data>
<date>周六(今天, 实时:22℃)date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/leizhenyu.pngdayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.pngnightPictureUrl>
<weather>雷阵雨转多云weather>
<wind>北风5-6级wind>
<temperature>24 ~ 11℃temperature>
<date>周日date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.pngdayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.pngnightPictureUrl>
<weather>多云转晴weather>
<wind>北风4-5级wind>
<temperature>19 ~ 8℃temperature>
<date>周一date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.pngdayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.pngnightPictureUrl>
<weather>晴weather>
<wind>微风wind>
<temperature>21 ~ 9℃temperature>
<date>周二date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.pngdayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.pngnightPictureUrl>
<weather>多云转晴weather>
<wind>微风wind>
<temperature>21 ~ 10℃temperature>
weather_data>
results>
CityWeatherResponse>
<web-app
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>bbservlet-name>
<servlet-class>yiche.com.servlet.WeiXinServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>bbservlet-name>
<url-pattern>/ok.dourl-pattern>
servlet-mapping>
web-app>
<dependencies>
<dependency>
<groupId>dom4jgroupId>
<artifactId>dom4jartifactId>
<version>1.6version>
dependency>
<dependency>
<groupId>com.thoughtworks.xstreamgroupId>
<artifactId>xstreamartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.7version>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20131018version>
dependency>
<dependency>
<groupId>com.github.iweinzierlgroupId>
<artifactId>jsonformatartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
dependency>
dependencies>
public class Weather {
private String date;
private String dayPictureUrl;
private String nightPictureUrl;
private String weather;
private String wind;
private String temperature;
}
public class Results {
private String currentCity;
private List weather_data;
}
public class Status {
private String error;
private String status;
private String date;
private List results;
}
public class Message {
private String ToUserName;
private String FromUserName;
private Long CreateTime;
private String MsgType;
private String Content;
private String MsgId;
}
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
try {
Map map = MessgaeUtils.xmlToMap(req);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
String message = null;
//MessageUtil.MESSAGE_TEXT就是 text
if(MessgaeUtils.MESSAGE_TEXT.equals(msgType)){
if(content.endsWith("天气")){
String keyWord = content.replaceAll("天气", "").trim();
message = MessgaeUtils.initText(toUserName, fromUserName, BaiDuWeather.getSend(keyWord));
}
}
out.print(message);
}catch (Exception e) {
// TODO: handle exception,
e.printStackTrace();
out.close();
}
}
public class BaiDuWeather {
private static String api = "http://api.map.baidu.com/telematics/v3/weather?";
private static String output = "json";
//你前面百度地图API上申请的key"
private static String ak ="81218080E79C9685b35e757566d8cbe5";
public static String getSend(String str) {
// 将传进来的城市转码
try {
str = URLEncoder.encode(str, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
// 拼接url
StringBuffer buf = new StringBuffer();
buf.append("location=");
buf.append(str);
buf.append("&output=");
buf.append(output);
buf.append("&ak=");
buf.append(ak);
String param = buf.toString();
// 这是接收百度API返回的内容
String result = "";
String urlName = api + param;
URL realUrl;
try {
realUrl = new URL(urlName);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// conn.setDoInput(true);
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream(),"UTF-8"));
String line = br.readLine();
while (line != null) {
result += line;
line = br.readLine();
}
br.close();
Gson gson = new Gson();
Status status = gson.fromJson(result, Status.class);
List results=status.getResults();
StringBuffer bf=new StringBuffer();
for(int i=0;iget (i);
List weather_data=res.getWeather_data();
for(int j=0;jget(j);
String date=weather.getDate();
String weath=weather.getWeather();
String temp=weather.getTemperature();
bf.append(date+" ");
bf.append(weath+" ");
bf.append(temp+" ");
bf.append("\n");
}
}
return bf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String json=getSend("湘西");
System.out.println(json);
}
}
public class MessgaeUtils {
public static final String MESSAGE_TEXT = "text";
public static final String MESSAGE_IMAGE = "image";
public static final String MESSAGE_VOICE = "voice";
public static final String MESSAGE_VIDEO = "video";
public static final String MESSAGE_SHORTVIDEO = "shortvideo";
public static final String MESSAGE_LINK = "link";
public static final String MESSAGE_LOCATION = "location";
public static final String MESSAGE_EVENT = "event";
public static final String MESSAGE_SUBSCRIBE = "subscribe";
public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe";
public static final String MESSAGE_CLICK = "CLICK";
public static final String MESSAGE_VIEW = "VIEW";
public static final String MESSAGE_SCAN = "SCAN";
//关注初始化消息内容
public static String menuText(){
StringBuffer b=new StringBuffer();
b.append("欢饮来到我的微信公众号,请选择\n\n");
b.append("1、我很帅。\n\n");
b.append("2、我贼鸡儿帅。\n\n");
b.append("回复、主菜单。\n\n");
return b.toString();
}
public static String initText(String toUserName,String fromUserName,String content){
Message message=new Message();
message.setToUserName(fromUserName);
message.setFromUserName(toUserName);
message.setMsgType(MESSAGE_TEXT);
message.setContent(content);
message.setCreateTime(new Date().getTime());
return objectToXml(message);
}
/*将xml格式转化为map*/
public static Map xmlToMap(HttpServletRequest request) throws Exception{
Map map=new HashMap<>();
SAXReader reader=new SAXReader();
InputStream inputStream=request.getInputStream();
Document doc=reader.read(inputStream);
Element root=doc.getRootElement();//得到根节点
List list=root.elements();//根节点下的所有的节点
for(Element e:list){
map.put(e.getName(),e.getText());
}
inputStream.close();
return map;
}
/*将我们的消息内容转变为xml*/
public static String objectToXml(Message message){
XStream xStream=new XStream();
xStream.alias("xml", message.getClass());
return xStream.toXML(message);
}
}