JAVA, 향후 6 일간 일기예보 입수
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
/**
* 6 ( )
* @author Chewl
*
*/
public class Weather {
String Ctiyid;
URLConnection connectionData;
StringBuilder sb;
BufferedReader br;// data
JSONObject jsonData;
JSONObject info;
public Weather(String Cityid) throws IOException ,NullPointerException{
// ip
this.Ctiyid = Cityid;
// API
URL url = new URL("http://m.weather.com.cn/data/" + Ctiyid + ".html");
connectionData = url.openConnection();
connectionData.setConnectTimeout(1000);
try {
br = new BufferedReader(new InputStreamReader(
connectionData.getInputStream(), "UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line);
} catch (SocketTimeoutException e) {
System.out.println(" ");
} catch (FileNotFoundException e) {
System.out.println(" ");
}
String datas = sb.toString();
jsonData = JSONObject.fromObject(datas);
// System.out.println(jsonData.toString());
info = jsonData.getJSONObject("weatherinfo");
// 1 6
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for(int i=1;i<=6;i++){
// 6
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, i-1);
Date date = cal.getTime();
SimpleDateFormat sf = new SimpleDateFormat("yyyy MM dd ");
Map<String,Object> map = new HashMap<String, Object>();
map.put("city", info.getString("city").toString());//
map.put("date_y", sf.format(date));//
map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));//
map.put("fchh", info.getString("fchh").toString());//
map.put("weather", info.getString("weather"+i).toString());//
map.put("temp", info.getString("temp"+i).toString());//
map.put("wind", info.getString("wind"+i).toString());//
map.put("fl", info.getString("fl"+i).toString());//
map.put("index", info.getString("index").toString());//
map.put("index_uv", info.getString("index_uv").toString());//
map.put("index_tr", info.getString("index_tr").toString());//
map.put("index_co", info.getString("index_co").toString());//
map.put("index_cl", info.getString("index_cl").toString());//
map.put("index_xc", info.getString("index_xc").toString());//
map.put("index_d", info.getString("index_d").toString());//
list.add(map);
}
//
for(int j=0;j<list.size();j++){
Map<String,Object> wMap = list.get(j);
System.out.println(wMap.get("city")+"\t"+wMap.get("date_y")+"\t"+wMap.get("week")+"\t"
+wMap.get("weather")+"\t"+wMap.get("temp")+"\t"+wMap.get("index_uv"));
}
}
private String getWeek(int iw){
String weekStr = "";
switch (iw) {
case 1:weekStr = " ";break;
case 2:weekStr = " ";break;
case 3:weekStr = " ";break;
case 4:weekStr = " ";break;
case 5:weekStr = " ";break;
case 6:weekStr = " ";break;
case 7:weekStr = " ";break;
default:
break;
}
return weekStr;
}
public static void main(String[] args) {
try {
new Weather("101010100"); // 101010100( )
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.