不完全版天气查询
来源:互联网 发布:java中wait怎么用 编辑:程序博客网 时间:2024/05/16 15:21
package com.zhidi.jsOn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;
import java.util.Scanner;
import org.json.JSONException;
import org.json.JSONObject;
public class Tianqi {
public static void main(String[] args) throws IOException, JSONException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入查询的地点:");
String str = sc.nextLine();
Tianqi.pd(str);
}
public static void pd(String code) throws IOException, JSONException {
switch (code) {
case "北京":
Tianqi.get("101010100");
break;
case "郑州":
Tianqi.get("101180101");
break;
case "洛阳":
Tianqi.get("101180901");
break;
}
}
public static void get(String code) throws IOException, JSONException {
String s1 = Tianqi.getweather("http://www.weather.com.cn/data/cityinfo/" + code + ".html");
String s2 = Tianqi.getweather("http://www.weather.com.cn/data/sk/" + code + ".html");
System.out.println(s1);
System.out.println(s2);
JSONObject obj = new JSONObject(s1);
JSONObject obj2 = new JSONObject(s2);
JSONObject o = (JSONObject) obj.get("weatherinfo");
JSONObject o2 = (JSONObject) obj2.get("weatherinfo");
System.out.println("地区:" + o.getString("city"));
System.out.println("温度:" + o.getString("temp1") + "~" + o.getString("temp2"));
System.out.println("天气:" + o.getString("weather"));
System.out.println("风向:" + o2.getString("WD"));
System.out.println("风级:" + o2.getString("WS"));
}
public static String getweather(String uri) throws IOException {
URL url = new URL(uri);
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String str;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
is.close();
return sb.toString();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;
import java.util.Scanner;
import org.json.JSONException;
import org.json.JSONObject;
public class Tianqi {
public static void main(String[] args) throws IOException, JSONException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入查询的地点:");
String str = sc.nextLine();
Tianqi.pd(str);
}
public static void pd(String code) throws IOException, JSONException {
switch (code) {
case "北京":
Tianqi.get("101010100");
break;
case "郑州":
Tianqi.get("101180101");
break;
case "洛阳":
Tianqi.get("101180901");
break;
}
}
public static void get(String code) throws IOException, JSONException {
String s1 = Tianqi.getweather("http://www.weather.com.cn/data/cityinfo/" + code + ".html");
String s2 = Tianqi.getweather("http://www.weather.com.cn/data/sk/" + code + ".html");
System.out.println(s1);
System.out.println(s2);
JSONObject obj = new JSONObject(s1);
JSONObject obj2 = new JSONObject(s2);
JSONObject o = (JSONObject) obj.get("weatherinfo");
JSONObject o2 = (JSONObject) obj2.get("weatherinfo");
System.out.println("地区:" + o.getString("city"));
System.out.println("温度:" + o.getString("temp1") + "~" + o.getString("temp2"));
System.out.println("天气:" + o.getString("weather"));
System.out.println("风向:" + o2.getString("WD"));
System.out.println("风级:" + o2.getString("WS"));
}
public static String getweather(String uri) throws IOException {
URL url = new URL(uri);
InputStream is = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String str;
StringBuilder sb = new StringBuilder();
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
is.close();
return sb.toString();
}
}
0 0
- 不完全版天气查询
- 天气查询
- 天气查询
- 查询天气
- 天气查询
- 天气查询
- 查询天气
- 查询天气
- PHP天气查询1
- PHP天气查询2
- VB实现天气查询
- AJAX 的天气查询
- 城市天气情况查询
- wp7查询天气
- 根据城市查询天气
- 天气查询接口
- 天气查询接口
- 天气查询接口
- mySQL数据库相关操作自学笔记三
- 仿京东轮播图
- iOS设计模式浅析之单例设计模式
- 一步一步实现大图预览
- 为什么GoogleNet中的Inception Module使用1*1 convolutions?
- 不完全版天气查询
- iOS设计模式浅析之抽象工厂设计模式
- 多版本Firefox共存
- 121.Best Time to Buy and Sell Stock
- SpringBoot Data JPA
- CodeIgniter 整合 UCenter 通信成功 登录不同步
- Linux C进程与多线程
- noip模拟题11.15 距noip2016还剩三天
- iOS设计模式浅析之装饰器模式