使用jsonp获取天气预报

本文介绍了如何利用jsonp技术获取心知天气的免费实时天气预报数据。在实践中,作者遇到了心知天气API的秘钥需要通过HMAC-SHA1加密的问题,并提供了解决方法,包括从网盘下载相关JS文件的步骤。最后,给出了完成此功能的代码示例。
摘要由CSDN通过智能技术生成

学完了jsonp后,总有种想用一下的冲动,于是上网找了一下天气api的接口,找了很久,终于找到了免费的,而且数据会实时更新。

我找到的这个就是心知天气,它提供免费的天气接口,可以让我来练一下手。

在练习的过程中,也碰到了许多坑。

比如,心知提供的秘钥还得经过HMAC-SHA1加密(谁叫咱是免费使用的),这个坑我进去了好久才爬出来,大家可以去我的网盘(密码:cpu3)进行下载,下载好了先引入jQ文件,然后再按次序引入如下几个文件。

1. core.js
2. cipher-core.js
3. enc-base64.js
4. hmac.js
5. sha1.js

引入完成后便可进行自己的代码块的编写。附上完整小demo代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width,user-scalable=no" />
        <title></title>
        <style type="text/css">
            *{
    margin:0;padding:0;box-sizing: border-box;}
            body{
    font-size:14px;}
            .container{
                width: 100%;
                min-width: 320px;
                max-width: 640px;
                margin: 0 auto;
            }
            .weather{
                clear: both;
                width: 100%;
                max-width: 640px;
                min-width: 320px;
                height: 300px;
                background: linear-gradient(to top, lightblue ,rgb(34, 174, 230));
                border-radius: 5px;
                padding: 10px;
                letter-spacing: 1px;
                color: #fff;
                position: relative;
            }
            .weather_now{
                text-align: center;
                /*margin: 10px 0;*/
                width: 100%;
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
天气查询APP,两种JSON解析方式 /** * 原始json数据解析 * */ // JSONObject jsonObject = new JSONObject(res); // String reason=jsonObject.getString("reason"); // if (reason.equals("参数不正确")){ // handler.sendEmptyMessage(1); // return; // } // JSONObject result=jsonObject.getJSONObject("result"); // JSONObject realtime=result.getJSONObject("realtime"); // JSONObject life=result.getJSONObject("life"); // JSONObject wind=realtime.getJSONObject("wind"); // String time=realtime.getString("time"); // JSONObject weather=realtime.getJSONObject("weather"); // String date=realtime.getString("date"); // dateStr=time+date; // weekStr=realtime.getString("week"); // calendarStr=realtime.getString("moon"); // windpowerStr=wind.getString("direct")+" "+wind.getString("power"); // weatherStr=weather.getString("info"); // temperatureStr=weather.getString("temperature"); // JSONObject info=life.getJSONObject("info"); // JSONArray kongtiao=info.getJSONArray("kongtiao"); // JSONArray yundong=info.getJSONArray("yundong"); // JSONArray ziwaixian=info.getJSONArray("ziwaixian"); // ACStr=kongtiao.getString(0)+" "+kongtiao.getString(1); // sportStr=yundong.getString(0)+" "+yundong.getString(1); // lightStr=ziwaixian.getString(0)+" "+ziwaixian.getString(1); /** * Gson数据解析 */ WheatherBean wheatherBean=new Gson().fromJson(res,WheatherBean.class); String reason=wheatherBean.getReason(); if (reason.equals("参数不正确")){ handler.sendEmptyMessage(1); return; } WheatherBean.ResultBean resultBean=wheatherBean.getResult(); WheatherBean.ResultBean.RealtimeBean realtimeBean=resultBean.getRealtime(); WheatherBean.ResultBean.RealtimeBean.WindBean windBean=realtimeBean.getWind(); String time=realtimeBean.getTime(); WheatherBean.ResultBean.RealtimeBean.WeatherBean weatherBean=realtimeBean.getWeather(); String date=realtimeBean.getDate(); dateStr=time+date; weekStr=realtimeBean.getWeek(); calendarStr=realtimeBean.getMoon(); windpowerStr=windBean.getDirect()+" "+windBean.getPower(); temperatureStr=weatherBean.getTemperature(); weatherStr=weatherBean.getInfo(); WheatherBean.ResultBean.LifeBean lifeBean=resultBean.getLife(); WheatherBean.ResultBean.LifeBean.InfoBean infoBean=lifeBean.getInfo(); List<String> kongtiao=infoBean.getKongtiao(); List<String> yundong=infoBean.getYundong(); List<String> ziwaixian=infoBean.getZiwaixian(); ACStr=kongtiao.get(0)+" "+kongtiao.get(1); sportStr=yundong.get(0)+" "+yundong.get(1); lightStr=ziwaixian.get(0)+" "+ziwaixian.get(1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值