</pre><pre name="code" class="java">public void run() {
// TODO Auto-generated method stub
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
conn.connect();
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String s;
StringBuffer sb = new StringBuffer();
while ((s = reader.readLine()) != null) {
sb.append(s);
}
reader.close();
in.close();
conn.disconnect();
Weather w = new Weather();
JSONObject jsonObject = new JSONObject(sb.toString());
JSONObject weatherinfo = jsonObject
.getJSONObject("weatherinfo");
w.setCity(weatherinfo.getString("city"));
String date = weatherinfo.getString("date_y");
w.setDate_y(date);
w.setIndex(weatherinfo.getString("index"));
w.setWind1(weatherinfo.getString("wind1"));
String week = weatherinfo.getString("week");
w.setWeek(week);
w.setTemp(weatherinfo.getString("temp1"));
w.setWeather(weatherinfo.getString("weather1"));
List<DayWeather> evday = new ArrayList<DayWeather>();
for (int i = 2; i < 7; i++) {
DayWeather d = new DayWeather();
String temp = weatherinfo.getString("temp" + i);
String weather = weatherinfo.getString("weather" + i);
String weekn = getNextWeek(week);
week = weekn;
String daten = getNextDate(date);
date=daten;
d.setDate_y(daten);
d.setWeek(weekn);
d.setTemp(temp);
d.setWeather(weather);
evday.add(d);
}
w.setEvday(evday);
Message msg = Message.obtain();
msg.obj = w;
msg.what = 1;
handler.sendMessage(msg);
} else {
Log.i("msg", "无法连接");
}
} catch (Exception e) {
// TODO: handle exception
}
super.run();
}
08-16