GetWeather.java源码
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;
public class GetWeather extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
Map map = CityUtil.getCity();
String cityName = new
String(req.getParameter("cityName").toString().getBytes("ISO-8859-1"),"UTF-8");
cityName = map.get(cityName);
String weatherURL =
"http://m.weather.com.cn/data/"+cityName+".html";
Weather weather = this.getWeather(weatherURL);
req.setAttribute("weather", weather);
req.getRequestDispatcher("/weather.jsp").forward(req,resp);
}
private Weather getWeather(String weatherURL) {
List returnedList = new ArrayList();
Weather weather = new Weather();
String jsonList =
NetworkUtil.getStringContentFromURL(weatherURL);
try {
JSONObject jsonStr = new JSONObject(jsonList);
JSONObject jsonObject =
jsonStr.getJSONObject("weatherinfo");
weather.setCityId(jsonObject.getString("cityid"));
weather.setCityName(jsonObject.getString("city"));
weather.setToday(jsonObject.getString("date_y"));
weather.setWeek(jsonObject.getString("week"));
weather.setTemp1(jsonObject.getString("temp1"));
weather.setTemp2(jsonObject.getString("temp2"));
weather.setTemp3(jsonObject.getString("temp3"));
weather.setTemp4(jsonObject.getString("temp4"));
weather.setTemp5(jsonObject.getString("temp5"));
weather.setTemp6(jsonObject.getString("temp6"));
weather.setWeather1(jsonObject.getString("weather1"));
weather.setWeather2(jsonObject.getString("weather2"));
weather.setWeather3(jsonObject.getString("weather3"));
weather.setWeather4(jsonObject.getString("weather4"));
weather.setWeather5(jsonObject.getString("weather5"));
weather.setWeather6(jsonObject.getString("weather6"));
weather.setWind1(jsonObject.getString("wind1"));
weather.setWind2(jsonObject.getString("wind2"));
weather.setWind3(jsonObject.getString("wind3"));
weather.setWind4(jsonObject.getString("wind4"));
weather.setWind5(jsonObject.getString("wind5"));
weather.setWind6(jsonObject.getString("wind6"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weather;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class NetworkUtil {
public static String getStringContentFromURL(String
url){
StringBuffer sb = new StringBuffer();
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try{
is = new URL(url).openConnection().getInputStream();
isr = new InputStreamReader(is, "UTF-8");
br = new BufferedReader(isr);
String line = null;
while(null != (line=br.readLine())){
sb.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null != br){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != isr){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
public class CityUtil {
public static Map getCity(){
Map map = new HashMap();
File f = new File("aa.txt");
try {
f.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
BufferedReader br = new BufferedReader(new
InputStreamReader(CityUtil.class.getResourceAsStream("/city.txt"),"UTF-8"));
String s = br.readLine();
while(s!=null){
String value = s.substring(0, s.indexOf('='));
//String key = new
String(s.substring(s.indexOf('=')+1).getBytes("ISO-8859-1"),"UTF-8");
String key = s.substring(s.indexOf('=')+1);
map.put(key, value);
s = br.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}