首页 > 代码库 > pm2.5 的获取 java
pm2.5 的获取 java
//石家庄铁道大学 tbh 基于云计算的pm2.5监控系统, 通过 api 获取 pm2.5的数据
//PM2.5数据以经由互联网抓取各城市的PM 2.5等数据,或者直接调用部分网站开放的API接口获取各城市的PM2.5等数据,如www.pm25.in 、等多个网站都开放了pm2.5的数据接口、天气等的实时查询API。
//包结构
//pm25.java
1 package com.tbh.entity;
2
3 public class pm25 {
4 //aqi指数
5 private String aqi;
6 //地域名称
7 private String area;
8 //颗粒物(粒径小于等于2.5μm)1小时平均
9 private String pm2_5;
10 //颗粒物(粒径小于等于2.5μm)24小时滑动平均
11 private String pm2_5_24h;
12 //监测点名称
13 private String position_name;
14 //首要污染物
15 private String primary_pollutant;
16 //空气质量指数类别,有“优、良、轻度污染、中度污染、重度污染、严重污染”6类
17 private String quality;
18 //监测点编码
19 private String station_code;
20 //数据发布的时间
21 private String time_point;
22 public String getAqi() {
23 return aqi;
24 }
25 public void setAqi(String aqi) {
26 this.aqi = aqi;
27 }
28 public String getArea() {
29 return area;
30 }
31 public void setArea(String area) {
32 this.area = area;
33 }
34 public String getPm2_5() {
35 return pm2_5;
36 }
37 public void setPm2_5(String pm2_5) {
38 this.pm2_5 = pm2_5;
39 }
40 public String getPm2_5_24h() {
41 return pm2_5_24h;
42 }
43 public void setPm2_5_24h(String pm2_5_24h) {
44 this.pm2_5_24h = pm2_5_24h;
45 }
46 public String getPosition_name() {
47 return position_name;
48 }
49 public void setPosition_name(String position_name) {
50 this.position_name = position_name;
51 }
52 public String getPrimary_pollutant() {
53 return primary_pollutant;
54 }
55 public void setPrimary_pollutant(String primary_pollutant) {
56 this.primary_pollutant = primary_pollutant;
57 }
58 public String getQuality() {
59 return quality;
60 }
61 public void setQuality(String quality) {
62 this.quality = quality;
63 }
64 public String getStation_code() {
65 return station_code;
66 }
67 public void setStation_code(String station_code) {
68 this.station_code = station_code;
69 }
70 public String getTime_point() {
71 return time_point;
72 }
73 public void setTime_point(String time_point) {
74 this.time_point = time_point;
75 }
76 @Override
77 public String toString() {
78 System.out.println( "pm25 [aqi=" + aqi + ", area=" + area + ", pm2_5=" + pm2_5
79 + ", pm2_5_24h=" + pm2_5_24h + ", position_name="
80 + position_name + ", primary_pollutant=" + primary_pollutant
81 + ", quality=" + quality + ", station_code=" + station_code
82 + ", time_point=" + time_point + "]");
83 return null;
84 }
85
86
87 }
//Pm25getjson.java
1 package com.tbh.services;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.net.URLConnection;
9
10 public class Pm25getjson {
11
12 private String json;
13 public Pm25getjson(String url)
14 {
15 StringBuilder jsonbuilder = new StringBuilder();
16 try {
17 URL urlObject = new URL(url);
18 URLConnection uc = urlObject.openConnection();
19 BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
20 String inputLine = null;
21 while ( (inputLine = in.readLine()) != null) {
22 jsonbuilder.append(inputLine);
23 }
24 in.close();
25 } catch (MalformedURLException e) {
26 e.printStackTrace();
27 } catch (IOException e) {
28 e.printStackTrace();
29 }
30 json=jsonbuilder.toString();
31 }
32 public String getJson() {
33 return json;
34 }
35 public void setJson(String json) {
36 this.json = json;
37 }
38
39 }
//pm25service.java
1 package com.tbh.services;
2
3 import org.json.JSONArray;
4 import org.json.JSONException;
5 import org.json.JSONObject;
6
7 import com.tbh.entity.pm25;
8
9
10 public class pm25service {
11
12
13
14
15 public pm25[] pm25arr_return(String url)
16 {
17
18 Pm25getjson a=new Pm25getjson(url);
19 String json=a.getJson();
20 JSONArray jsonArray = null;
21 try {
22 jsonArray = new JSONArray(json);
23 } catch (JSONException e) {
24 // TODO Auto-generated catch block
25 e.printStackTrace();
26 }
27 int iSize = jsonArray.length();
28 pm25[] pm25arr=new pm25[iSize];
29 System.out.println("Size:" + iSize);
30 for (int i = 0; i < iSize; i++) {
31 pm25arr[i]=new pm25();
32
33 try {
34 System.out.println(jsonArray.getJSONObject(i).get("aqi").toString());
35 pm25arr[i].setAqi(jsonArray.getJSONObject(i).get("aqi").toString());
36 } catch (JSONException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 }
40 try {
41 pm25arr[i].setArea(jsonArray.getJSONObject(i).get("area").toString());
42 } catch (JSONException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 }
46 try {
47 pm25arr[i].setPm2_5(jsonArray.getJSONObject(i).get("pm2_5").toString());
48 } catch (JSONException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52 try {
53 pm25arr[i].setPm2_5_24h(jsonArray.getJSONObject(i).get("pm2_5_24h").toString());
54 } catch (JSONException e) {
55 // TODO Auto-generated catch block
56 e.printStackTrace();
57 }
58 try {
59 pm25arr[i].setPrimary_pollutant(jsonArray.getJSONObject(i).get("position_name").toString());
60 } catch (JSONException e) {
61 // TODO Auto-generated catch block
62 e.printStackTrace();
63 }
64 try {
65 pm25arr[i].setQuality(jsonArray.getJSONObject(i).get("quality").toString());
66 } catch (JSONException e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }
70 try {
71 pm25arr[i].setStation_code(jsonArray.getJSONObject(i).get("station_code").toString());
72 } catch (JSONException e) {
73 // TODO Auto-generated catch block
74 e.printStackTrace();
75 }
76 try {
77 pm25arr[i].setTime_point(jsonArray.getJSONObject(i).get("time_point").toString());
78 } catch (JSONException e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82
83
84 }
85 return pm25arr;
86
87
88
89
90
91
92
93
94
95
96 }
97
98 }
//pm25test.java
package com.tbh.test;
import com.tbh.entity.pm25;
import com.tbh.services.Pm25getjson;
import com.tbh.services.pm25service;
public class pm25test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "http://www.pm25.in/api/querys/pm2_5.json?city=weinan&token=5j1znBVAsnSf5xQyNQyq";
pm25service pm25services=new pm25service();
pm25[] pm25entity=pm25services.pm25arr_return(url);
for(int i=0;i<pm25entity.length;i++)
{
System.out.println( pm25entity[i].getAqi());
System.out.println( pm25entity[i].getArea());
System.out.println( pm25entity[i].getPm2_5());
System.out.println( pm25entity[i].getPm2_5_24h());
System.out.println( pm25entity[i].getPosition_name());
System.out.println( pm25entity[i].getPrimary_pollutant());
System.out.println( pm25entity[i].getQuality());
System.out.println( pm25entity[i].getStation_code());
System.out.println( pm25entity[i].getTime_point());
System.out.println();
}
/* Pm25getjson a=new Pm25getjson(url);
System.out.println(a.getJson());*/
}
}
pm2.5 的获取 java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。