下面是一些可用的接口
1.https://www.tianqiapi.com/api/?version=v6&&city=杭州
2.http://api.help.bj.cn/apis/weather36h/?id=杭州 http://api.help.bj.cn/api/
3.http://api.k780.com:88/?app=weather.history&weaid=hangzhou&date=2019-07-11&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json
4.http://www.weather.com.cn/data/sk/101210101.html
5.http://www.weather.com.cn/data/cityinfo/101010100.html
6.http://www.webxml.com.cn/WebServices/WeatherWS.asmx
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class weatherUtil {
private String weather; //天气状况
private String tempertureNow; //当前温度
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getTempertureNow() {
return tempertureNow;
}
public void setTempertureNow(String tempertureNow) {
this.tempertureNow = tempertureNow;
}
@Override
public String toString() {
return "weatherUtil{" +
"weather='" + weather + '\'' +
", tempertureNow='" + tempertureNow + '\'' +
'}';
}
//日志
private static Logger log = Logger.getLogger(now.class);
/**
* 获取天气JSON
* @param cityName
* @return
*/
public static String getWeatherInform(String cityName) {
String baiduUrl = "http://api.help.bj.cn/apis/weather36h/?id=杭州";
StringBuffer strBuf;
try {
baiduUrl = "http://api.help.bj.cn/apis/weather36h/?id=" + URLEncoder.encode(cityName, "utf-8"); //接口1
// baiduUrl = "https://www.tianqiapi.com/api/?version=v6&&city="+ URLEncoder.encode(cityName, "utf-8"); //接口2
} catch (Exception e1) {
log.error(e1.getMessage());
}
strBuf = new StringBuffer();
try {
URL url = new URL(baiduUrl);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));// 转码。
String line = null;
while ((line = reader.readLine()) != null)
strBuf.append(line + " ");
reader.close();
} catch (MalformedURLException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
return strBuf.toString();
}
static weatherUtil resolveWeatherInf(String strPar){
JSONObject dataOfJson = JSONObject.parseObject(strPar);
if(dataOfJson.getIntValue("error")!=0){
return null;
}
//保存全部的天气信息
weatherUtil wea = new weatherUtil();
JSONArray results=dataOfJson.getJSONArray("weather36h");
JSONObject results0=results.getJSONObject(0);
wea.setWeather(results0.getString("weather"));
wea.setTempertureNow(results0.getString("temp"));
//接口2
/* try {
wea.setWeather(new String(dataOfJson.getString("wea").getBytes(),"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
wea.setTempertureNow(dataOfJson.getString("tem"));*/
return wea;
}
public static void main(String[] args) {
String n=getWeatherInform("杭州");
log.info(n);
weatherUtil w=resolveWeatherInf(n);
log.info(w);
}
}
结果
这个地址的例子 http://www.webxml.com.cn/WebServices/WeatherWS.asmx
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
/**
* 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市
*
* @param city
* 用户输入的城市名称
* @return 客户将要发送给服务器的SOAP请求
*/
private static String getSoapRequest(String city) {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body> <getWeather xmlns=\"http://WebXml.com.cn/\">" + "<theCityCode>" + city
+ "</theCityCode> </getWeather>" + "</soap:Body></soap:Envelope>");
return sb.toString();
}
/**
* 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流
*
* @param city
* 用户输入的城市名称
* @return 服务器端返回的输入流,供客户端读取
* @throws Exception
*/
private static InputStream getSoapInputStream(String city) throws Exception {
try {
String soap = getSoapRequest(city);
if (soap == null) {
return null;
}
URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx");
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeather");
OutputStream os = conn.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
osw.write(soap);
osw.flush();
osw.close();
InputStream is = conn.getInputStream();
return is;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 对服务器端返回的XML进行解析
*
* @param city
* 用户输入的城市名称
* @return 字符串 用#分割
*/
public static String getWeather(String city) {
try {
Document doc;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = getSoapInputStream(city);
doc = db.parse(is);
NodeList nl = doc.getElementsByTagName("string");
StringBuffer sb = new StringBuffer();
for (int count = 0; count < nl.getLength(); count++) {
Node n = nl.item(count);
if (n.getFirstChild().getNodeValue().equals("查询结果为空!")) {
sb = new StringBuffer("#");
break;
}
sb.append(n.getFirstChild().getNodeValue() + "#");
}
is.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 测试
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String weatherInfo = getWeather("杭州");
System.out.println(weatherInfo);
}
}
调用百度天气接口的一篇博客:https://blog.csdn.net/jason763/article/details/52789462