java接口获取天气预报json数据(只为转载学习)

原文链接地址

获取到JAVA调用HTTP接口的方法

package wzgr;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
 
 
 
public class HttpRequest {
	
 	public static void main(String[] args) {
		
		String b=HttpRequest.sendGet("http://www.XXXX.com/......");
		System.out.println(b);
	}


    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader
                (new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
 
}

二、

使用天气接口,接口获取天气,后面的101170301为城市编号。各城市编号连接下载地址"https://download.csdn.net/download/weixin_38959210/10793554";

@RequestMapping(value = "/tianqi.do")
	public JSONObject tianqi(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		System.out.println("/tianqi.do打印出的东西:"+cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301"));
		 
		 return (JSONObject) JSON.parse(cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301")); 
 
	}

以上为后台代码:

前台html代码为:

<table border="1">
  <tr>
    <td>城市</td>
    <td id=city></td>
  </tr>
  <tr>
    <td>更新时间</td>
    <td id=updateTime></td>
  </tr>
  <tr>
    <td>湿度</td>
    <td id=shidu></td>
  </tr>
  <tr>
    <td>pm2.5</td>
    <td id=pm25></td>
  </tr>
  <tr>
    <td>pm10</td>
    <td id=pm10></td>
  </tr>
  <tr>
    <td>空气质量</td>
    <td id=quality></td>
  </tr>
  <tr>
    <td>温度</td>
    <td id=wendu></td>
  </tr>
  <tr>
    <td>空气指数</td>
    <td id=ganmao></td>
  </tr>
</table>

js代码:

$(function(){ 
 
 
function tianqi(){
	$.ajax({
		 url: "tianqi.do",
		 type: "POST",
		 datatype: "json", 
		 contentType: "application/json", 
		 success: function(data){
			 $("#time").html(data.time);
			 $("#city").html(data.cityInfo.city);
			 $("#updateTime").html(data.cityInfo.updateTime);
			 $("#shidu").html(data.data.shidu);
			 $("#pm25").html(data.data.pm25);
			 $("#pm10").html(data.data.pm10);
			 $("#quality").html(data.data.quality);
			 $("#wendu").html(data.data.wendu);
			 $("#ganmao").html(data.data.ganmao);
		 },
		 error:function(){
			 alert("获取数据失败");
		 }
	});
		
}

## 三
	天气API(需注册获取key):https://www.sojson.com/blog/305.html
## 四
	免费API(无需注册,不知道稳不稳定):http://t.weather.itboy.net/api/weather/city/101121301
	demo地址:https://www.sojson.com/blog/349.html	



看完文章了可以试试下面这个接口:

http://i.tianqi.com/index.php?c=code&id=12&icon=1&num=5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值