API调用开发demo
阅读原文时间:2023年07月10日阅读:1

package fastjson;

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.net.URLConnection;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/*
* 1.URLConnection 的使用
*
* 2.fastJson 的使用
*
*/
public class connAPI {
//xml格式数据
public static void xml() throws Exception{
//参数url化
String city = java.net.URLEncoder.encode("北京", "utf-8");
//拼地址
String apiUrl = String.format("https://www.sojson.com/open/api/weather/xml.shtml?city=%s",city);
//开始请求
URL url= new URL(apiUrl);
URLConnection open = url.openConnection();
InputStream input = open.getInputStream();
//这里转换为String,带上包名,怕你们引错包
String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
//输出
System.out.println(result);
}

//json 格式
public static void json() throws Exception{
//参数url化
String city = java.net.URLEncoder.encode("北京", "utf-8");
//拼地址
String apiUrl = String.format("https://www.sojson.com/open/api/weather/json.shtml?city=%s",city);
//开始请求
URL url= new URL(apiUrl);
URLConnection open = url.openConnection();
InputStream input = open.getInputStream();
//这里转换为String,带上包名,怕你们引错包
String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
//输出
System.out.println(result);
}
public static void fastjson(){
String neturl = "https://www.sojson.com/open/api/weather/json.shtml?city=%E5%8C%97%E4%BA%AC";
URL url;
try {
url = new URL(neturl);
StringBuffer document = new StringBuffer();
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line);
}
reader.close();
// {"date":"20180921","message":"Success !","status":200,"city":"北京","count":2,
// "data":{"shidu":"28%","pm25":13.0,"pm10":23.0,"quality":"优","wendu":"19","ganmao":"各类人群可自由活动",
// "yesterday":{"date":"20日星期四","sunrise":"05:58","high":"高温 27.0℃","low":"低温 16.0℃","sunset":"18:17","aqi":54.0,"fx":"南风","fl":"<3级","type":"多云","notice":"阴晴之间,谨防紫外线侵扰"},
// "forecast":[{"date":"21日星期五","sunrise":"05:59","high":"高温 25.0℃","low":"低温 14.0℃","sunset":"18:15","aqi":34.0,"fx":"西北风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"22日星期六","sunrise":"06:00","high":"高温 23.0℃","low":"低温 14.0℃","sunset":"18:13","aqi":40.0,"fx":"西北风","fl":"3-4级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"23日星期日","sunrise":"06:01","high":"高温 23.0℃","low":"低温 12.0℃","sunset":"18:12","aqi":30.0,"fx":"北风","fl":"4-5级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"24日星期一","sunrise":"06:02","high":"高温 22.0℃","low":"低温 11.0℃","sunset":"18:10","aqi":37.0,"fx":"北风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},
// {"date":"25日星期二","sunrise":"06:03","high":"高温 23.0℃","low":"低温 13.0℃","sunset":"18:09","aqi":52.0,"fx":"西南风","fl":"<3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"}]}}

JSONObject jsonObject = JSON.parseObject(document.toString());
String date = jsonObject.getString("date");
String message = jsonObject.getString("message");
Integer status = jsonObject.getInteger("status");
String city = jsonObject.getString("city");
// 解析后面
String datas = jsonObject.getString("data");
JSONObject jb = JSON.parseObject(datas);
String shidu = jb.getString("shidu");
String wendu = jb.getString("wendu");
String ganmao = jb.getString("ganmao");
Integer pm25 = jb.getInteger("pm25");
Integer pm10 = jb.getInteger("pm10");
String quality = jb.getString("quality");

System.out.println(document);
System.out.println(date);
System.out.println(message);
System.out.println(status);
System.out.println(city);
System.out.println(datas);
System.out.println(shidu);
System.out.println(pm25);
System.out.println(pm10);
System.out.println(quality);
System.out.println(shidu);
System.out.println(wendu);
System.out.println(ganmao);

JSONArray forecast = jb.getJSONArray("forecast");
System.out.println(forecast);
for (int i = 0; i < forecast.size(); i++) {
JSONObject json = forecast.getJSONObject(i);
System.out
.println(json.getString("fl") + ":"
+ json.getString("fx") + ":"
+ json.getString("notice"));
}
String yesterday = jb.getString("yesterday");
JSONObject yjb = JSON.parseObject(yesterday);
System.out.println(yjb.toString());
String date2 = yjb.getString("date");
String sunrise = yjb.getString("sunrise");
System.out.println(date2);
System.out.println(sunrise);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//fastjson();
try {
//xml();
json();
} catch (Exception e) {
e.printStackTrace();
}
}
}