天气api(天气api是什么意思)

大雄 267 2022-10-25

本文目录一览:

彩云天气API怎么获取

腾讯位置服务平台提供获取经纬度的接口

彩云天气申请开发者api调用对应经纬度的天气状况

测试环境选择了自己的iphone上的JSbox来运行一个简单的js脚本:

//简单思路就是 获取ip再获取天气信息

const locationKey = "XXXXXXXXXXXXX"

const weatherKey = "XXXXXXXXXXXX"

const apiList = {

location:""

}

function getLonLat(){

$http.get({

url: `${apiList.location}/location/v1/ip?key=${locationKey}`,

handler: (resp) = {

let location = resp.dataresp.data.resultresp.data.result.location

getLocation(location)

}

});

}

function getLocation(location){

$http.get({

url: `${apiList.location}/geocoder/v1/?key=${locationKey}location=${location.lat},${location.lng}`,

handler: (resp) = {

var data = resp.data;

$console.info(data.result.formatted_addresses.recommend);

}

});

}

/**

*

* @param {lat:"",lng:""} location

*/

function getWeather(location){

$http.get({

url: `${apiList.weather}/${weatherKey}/${location.lng},${location.lat}/weather.json`,

handler: (resp) = {

let data = resp.data;

console.info(data)

//运行结果参照彩云天气

}

});

}

getLonLat()

2|0顿时醒悟

写到这其实我只是想测试一下两个接口的基本用法以及可用之处,然后突然想到jsbox里面内置的$location可以直接获取到设备的位置信息,通过这样获取到的位置坐标会比ip的更加精准

//根据原生SDK获取手机位置

function getPhoneLoc(){

$location.fetch({

handler: function(resp) {

var lat = resp.lat;

var lng = resp.lng;

var alt = resp.alt;

let loc = {lat:lat,lng:lng}

getLocation(loc)

}

});

}

3|0最后运行结果

java调用天气预报api怎么写

//通过中国天气api调用

private String getWeatherInfo2(){

StringBuilder info = new StringBuilder();

try {

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpGet httget = new HttpGet("");

ResponseHandlerString responseHandler = new BasicResponseHandler();

String responseBody = httpclient.execute(httget, responseHandler);

System.out.println(responseBody);

JsonParser jp = new JsonParser();

JsonElement jse = jp.parse(responseBody);

JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();

// String updTime = jso.get("fchh").getAsString();

// if(updTime != null){

// //温度

// String j = jso.get("temp1").getAsString();//今天

// String m = jso.get("temp2").getAsString();//明天

// //天气情况

// String j_weather = jso.get("weather1").getAsString();//今天

// String m_weather = jso.get("weather2").getAsString();//明天

// //风向风力

// String j_wind = jso.get("wind1").getAsString();//今天

// String m_wind = jso.get("wind2").getAsString();//明天

// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");

// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");

// }

String updTime = jso.get("fchh").getAsString();

if(updTime != null){

if(!updTime.trim().equals("18")){

//温度

String j = jso.get("temp1").getAsString();//今天

String m = jso.get("temp2").getAsString();//明天

//天气情况

String j_weather = jso.get("weather1").getAsString();//今天

String m_weather = jso.get("weather2").getAsString();//明天

//风向风力

String j_wind = jso.get("wind1").getAsString();//今天

String m_wind = jso.get("wind2").getAsString();//明天

info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");

info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");

}else{

//18

//温度

String temp1 = jso.get("temp1").getAsString();//今天

String temp2 = jso.get("temp2").getAsString();//今天

String temp3 = jso.get("temp3").getAsString();//今天

String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];

String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天

//天气情况

String weather1 = jso.get("weather1").getAsString();

String weather2 = jso.get("weather2").getAsString();

String weather3 = jso.get("weather3").getAsString();

String j_weather = "";

String j_weather_part1 = "";

String j_weather_part2 = "";

//判断是否有转

if(weather1.indexOf("转")  0){

//有

j_weather_part1 = weather1.split("转")[1];

}else{

j_weather_part1 = weather1;

}

if(weather2.indexOf("转")  0){

//有

j_weather_part2 = weather2.split("转")[0];

}else{

j_weather_part2 = weather2;

}

if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){

j_weather = j_weather_part1;//今天

}else{

j_weather = j_weather_part1 + "转" + j_weather_part2;//今天

}

String m_weather = "";

String m_weather_part1 = "";

String m_weather_part2 = "";

//判断是否有转

if(weather2.indexOf("转")  0){

//有

m_weather_part1 = weather2.split("转")[1];

}else{

m_weather_part1 = weather2;

}

if(weather3.indexOf("转")  0){

//有

m_weather_part2 = weather3.split("转")[0];

}else{

m_weather_part2 = weather3;

}

if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){

m_weather = m_weather_part1;//今天

}else{

m_weather = m_weather_part1 + "转" + m_weather_part2;//明天

}

//风向风力

String j_wind = jso.get("wind2").getAsString();//今天

String m_wind = jso.get("wind3").getAsString();//明天

info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");

info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");

}

}

} catch (Exception e) {

}

return info.toString();

}

天气预报API接口是什么?

天气预报API接口是气象服务商为客户提供服务的唯一接口。拿墨迹赤必为例,跟客户充分沟通后了解客户的实际需求,从而对现有的数据进行调整,符合客户的需求形成API接口,让客户接入,这样就形成了一个完整的服务。所以说API接口并不是一个很神秘的东西,只不过是企业和客户之间的一条服务纽带。

如何使用百度天气预报API接口

百度API Key申请地址:

创建应用 如图: 

提交后得到API Key 如图:

接口实例:?location=南昌output=jsonak=你的API Keymcode=你的数字签名SHA1;com.example.administrator.jsontest(包名)

接口参数说明

参数类型

参数名称

是否必须

具体描述

String    location    true    输入城市名或经纬度,城市名如北京或者131,经纬度格式为lng,lat坐标,如:location=116.305145,39.982368;全国值为all,返回省会城市自治区,港澳台天气情况多城市天气预报中间用“|”分隔,如:location=116.305145,39.982368|123.342323,36238945|...  

String    output    false    输出的数据格式,默认为xml格式,当output设置为json时,输出的为json数据格式  

String    coord_type    false    请求参数坐标类型,默认为gcj02经纬度坐标。允许的值为bd09ll、bd09mc、gcj02、wgs84;  

返回的JSON数据

{

   "error":0,

   "status":"success",

   "date":"2016-03-05",

   "results":[

       {

           "currentCity":"北京",

           "pm25":"144",

           "index":[

               {

                   "title":"穿衣",

                   "zs":"较冷",

                   "tipt":"穿衣指数",

                   "des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},

               {

                   "title":"洗车",

                   "zs":"不宜",

                   "tipt":"洗车指数",

                   "des":"不宜洗车,未来24小时内有扬沙或浮尘,如果在此期间洗车,极易很快蒙上新的灰尘。"},

               {

                   "title":"旅游",

                   "zs":"一般",

                   "tipt":"旅游指数",

                   "des":"风稍大,扬沙或浮尘天气对能见度和空气质量都会有些影响,出行请注意交通安全和采取适当的防尘措施。"},

               {

                   "title":"感冒",

                   "zs":"易发",

                   "tipt":"感冒指数",

                   "des":"昼夜温差大,风力较强,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"},

               {

                   "title":"运动",

                   "zs":"较不宜",

                   "tipt":"运动指数",

                   "des":"有扬沙或浮尘,建议适当停止户外运动,选择在室内进行运动,以避免吸入更多沙尘,有损健康。"},

               {

                   "title":"紫外线强度",

                   "zs":"最弱",

                   "tipt":"紫外线强度指数",

                   "des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}

          ],

 "weather_data":[

             {

                 "date":"周六 03月05日 (实时:12℃)",              "dayPictureUrl":"",

   "nightPictureUrl":"",

                 "weather":"浮尘转晴",

                 "wind":"北风4-5级",

                 "temperature":"12 ~ -1℃"},

 {

                 "date":"周日",

"dayPictureUrl":"",

"nightPictureUrl":"",

                 "weather":"多云",

                 "wind":"微风",

                 "temperature":"10 ~ -3℃"},

 {

                 "date":"周一",            "dayPictureUrl":"",

"nightPictureUrl":"",

                 "weather":"多云转阴",

                 "wind":"微风",

                 "temperature":"13 ~ 2℃"},

 {

                 "date":"周二",                "dayPictureUrl":"",

"nightPictureUrl":"",

             "weather":"阴转多云",

             "wind":"北风3-4级",

             "temperature":"6 ~ -1℃"}

 ]}]}

3.  我们来写个demo,代码如下:

package com.example.administrator.jsontest;

public class MainActivity extends Activity {

   private Button button;

   private TextView textView;

   private Handler handler = new Handler() {

       @Override

       public void handleMessage(Message msg) {

           switch (msg.what) {

               case 0:

                   String re = (String) msg.obj;

                   textView.setText(re);

                   break;

           }

       }

   };

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       button = (Button) findViewById(R.id.button);

       textView = (TextView) findViewById(R.id.textView);

       button.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View v) {

               Log.i("TAG", "点击了Button");

               sendRequestWithHttpClient();

           }

       });

   }

   private void sendRequestWithHttpClient() {

       new Thread(new Runnable() {

           @Override

           public void run() {

               HttpURLConnection connection = null;

               try {

                   URL url = new URL("南昌output=jsonak=8ixCCFzlBB617YX7tONI2P5Bmcode=1C:6B:42:33:E8:A6:DC:A2:11:6E:26:EC:84:BD:42:E3:8E:6B:57:9A;com.example.administrator.jsontest");

                   connection = (HttpURLConnection) url.openConnection();

                   connection.setRequestMethod("GET");

                   connection.setConnectTimeout(5000);

                   connection.setReadTimeout(5000);

                   InputStream in = connection.getInputStream();

                   BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                   StringBuilder response = new StringBuilder();

                   String line;

                   while ((line = reader.readLine()) != null) {

                       response.append(line);

                   }

                   Log.i("TAG", response.toString());                    parseJSONObjectOrJSONArray(response.toString());

               } catch (MalformedURLException e) {

                   e.printStackTrace();

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }

       }).start();

   }

   //解析JSON数据

   private void parseJSONObjectOrJSONArray(String jsonData) {

       try {

           String count = "";

           JSONObject jsonObject = new JSONObject(jsonData);

           JSONArray jsonArray = jsonObject.getJSONArray("results");

           if (jsonArray.length()  0) {

               JSONObject object = jsonArray.getJSONObject(0);

               String city = object.optString("currentCity");

               JSONArray array = object.getJSONArray("weather_data");

               for (int i = 0; i  array.length(); i++) {

                   JSONObject jsonObject1 = array.getJSONObject(i);

                   String dateDay = jsonObject1.optString("date");

                   String weather = jsonObject1.optString("weather");

                   String wind = jsonObject1.optString("wind");

                   String temperature = jsonObject1.optString("temperature");

                   count =count +"\n"+ dateDay + " " + weather + " " + wind + " " + temperature;

                   Log.i("AAA",count);

               }

               Message message = new Message();

               message.what = 0;

               message.obj = count;

               handler.sendMessage(message);

           }

       } catch (JSONException e) {

           e.printStackTrace();

       }

   }

}

4. 运行结果如下:


墨迹赤必提供的天气预报API接口有什么作用?


这块东西往专业来说肯定大部分人不是太了解,我可以给你举个例子,如何让两个平行的路连起来了?很简单,搭个桥就可以实现两条路的互通,而墨迹赤必提供的API接口就是这样一个作用。因为墨迹赤必拥有着丰富的天气数据,而客户需要的是更加精准的需求,两相结合之后需要有一个连结的东西,而墨迹赤必的API接口就起着这样的作用。我想这样说应该能明白吧。

调用百度天气的api时候跨域的问题

同源的问题,目前网上没有很好的解决百度天气api的方案。个人目前实行的方式是,先将api数据的通过file_get_contents获取JSON字符串,然后再用ajax调用.php函数file_get_contents应对百度天气接口


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:运营商二要素(运营商二要素查询)
下一篇:浅析USB接口的保护方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~