可以在JFrame中添加幾個Label控件或其他文本控件用來展示解析出來的結(jié)果
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的安溪網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
氣溫高,則氣壓低,因為高氣溫使空氣膨脹,使相對下的空氣密度變大. 氣溫高,氣壓低,濕度大時,一般會下雨. 有霧時,氣溫高,蒸發(fā)量大,一般是晴天. 氣溫適中時,一般是陰天,因為陰天時云比較多,大量的云反射了太陽的大部分熱量,同時也逆反射了地球的熱量,這樣就起到了保溫作用,溫度就不會太熱,也不會太低
①取出地址中的返回值(getWeatherReader方法)
②解析json格式的字符串
③形成你要展示的天氣預(yù)報效果
public static String getWeatherReader() {//取得接口字符串
String currentLine = "";
String strReturn = "";
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
BufferedReader buff = null;
try {
url = new URL("");
System.out.println(url.toURI());
//打開地址鏈接
conn = (HttpURLConnection)url.openConnection();
conn.connect();
//接收數(shù)據(jù)
in = conn.getInputStream();
//如有亂碼注意編碼方式,如:UTF-8
buff = new BufferedReader(new InputStreamReader(in, "gb2312"));
while((currentLine = buff.readLine()) != null) {
strReturn += currentLine;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
buff.close();
} catch (IOException e) {
return "8EF0000";
}
}
return strReturn;
}
//通過中國天氣api調(diào)用
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("");
ResponseHandlerString responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //溫度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天氣情況
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //風(fēng)向風(fēng)力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//溫度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天氣情況
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//風(fēng)向風(fēng)力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//溫度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天氣情況
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判斷是否有轉(zhuǎn)
if(weather1.indexOf("轉(zhuǎn)") 0){
//有
j_weather_part1 = weather1.split("轉(zhuǎn)")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("轉(zhuǎn)") 0){
//有
j_weather_part2 = weather2.split("轉(zhuǎn)")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "轉(zhuǎn)" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判斷是否有轉(zhuǎn)
if(weather2.indexOf("轉(zhuǎn)") 0){
//有
m_weather_part1 = weather2.split("轉(zhuǎn)")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("轉(zhuǎn)") 0){
//有
m_weather_part2 = weather3.split("轉(zhuǎn)")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "轉(zhuǎn)" + m_weather_part2;//明天
}
//風(fēng)向風(fēng)力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}
import?java.util.Scanner;
public?class?Weather?{
public?static?String?getWeatherInChinese(char?c)?{
switch?(c)?{
case?'D':
return?"干燥";
case?'M':
return?"潮濕";
case?'H':
return?"炎熱";
case?'R':
return?"下雨";
default:
return?"輸入錯誤,無法轉(zhuǎn)換";
}
}
public?static?void?main(String[]?args)?{
boolean?tbd?=?true;
do?{
System.out.print("請輸入天氣情況的第一個英文字母:");
Scanner?scanner?=?new?Scanner(System.in);
String?input?=?scanner.next().trim();
if?(input.length()?!=?1)?{
System.out.println("輸入錯誤,無法轉(zhuǎn)換!");
}?else?{
System.out.println(getWeatherInChinese(input.charAt(0)));
}
System.out.print("你想繼續(xù)嗎?(y/n)");
String?sta?=?scanner.next();
tbd?=?sta.equals("y")???true?:?false;
}?while?(tbd);
System.out.println("退出系統(tǒng)!");
}
}
運行截圖: