调用接口,首先得去注册一个账号,这个账号是我同事注册的,我也只在这里提一下(它只能获取当天的数据,没有百度API的功能强大),但是JAVA实现的方法和思路都是一样的
package com.common.filo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class weatherApi {
public static void main(String[] args) {
//31.874000,101.110880,3362.300
String weidu = "101.110880";
String jindu = "31.874000";
String weatherData = getWeatherData(weidu,jindu);
List getWeather = GetWeather(weatherData);
for (Object object : getWeather) {
System.out.println(object.toString());
}
}
/**
* 根据经纬度获取城市的气温信息
* @param weidu 纬度
* @param jindu 经度
* @return
*/
public static String getWeatherData(String weidu,String jindu) {
StringBuilder sb = new StringBuilder();
//http://api.yytianqi.com/weatherhours?key=bopi3li1ip93ae0n&city=31.874000,101.110880"
try {
String weather_url = "http://api.yytianqi.com/weatherhours?key=bopi3li1ip93ae0n&city="+weidu+","+jindu+"";
URL url = new URL(weather_url);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = null;
while((line=reader.readLine())!=null) {
sb.append(line+" ");
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 对调用天气API的数据进行json序列化
* @param weatherJson
* @return
*/
public static List<String> GetWeather(String weatherJson) {
//城市名
String cityname= "";
//获取当时的气温
String qw = "";
List<String> list = new ArrayList<String>();
JSONObject dataJson = JSONObject.fromObject(weatherJson.trim());
String getCode = dataJson.getString("msg"