本文记录测试网络上免费的天气api,只测试两个。
百度一下有很多,可以自己去找别的。
例如我做测试的APIX与APIStore。还有易源接口,聚合等等。。。。
首先给出百度API的地址http://apistore.baidu.com/apiworks/servicedetail/112.html
测试根据城市名称查询天气的,另外还有历史7天与未来3天,根据城市拼音或者城市id等其它接口不做测试
package com.lhh.Weather;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class BaiduWeather {
/*
* 根据城市名称查询天气
* @param city
*
* auther 老辉辉
*/
//百度天气api上给的可用,免费apikey d2ae44152f93b617584f9a6e8740c94e
private static String mykey = "d2ae44152f93b617584f9a6e8740c94e";
private static String httpUrl = "http://apis.baidu.com/apistore/weatherservice/cityname";
public static void main(String[] args) {
// TODO Auto-generated method stub
String city = "福州";
String httpArg = "cityname="+city+"";
String jsonResult = request(httpUrl, httpArg);
System.out.println(jsonResult);
}
//向服务器发送请求,并传递参数
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?" + httpArg;
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("apikey", mykey);
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
以下是C#语言写的(窗体应用程序)
1.核心文件Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.IO;
namespace 天气查询
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//我看他的代码自己理解后写了一个。得到的是Json格式的数据,未拆分。
//http://a.apix.cn/apixlife/weather/weather?cityname=福州
System.Net.WebRequest req = System.Net.WebRequest.Create("http://a.apix.cn/apixlife/weather/weather?cityid=101230101");
req.Method = "GET";
req.ContentType = "application/json";
//1fa54fddcb5d4f777a76949f2596574e ,这个是我注册后给的ID
req.Headers.Add("apix-key", "d0e89b0e52f64f38435dcb673ba23d7e");
//req.Headers.Add("accept", "application/json");
System.Net.WebResponse resp= req.GetResponse();
System.IO.Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
textBox1.Text = sr.ReadToEnd();
sr.Close();
//下面是帮助文档的示例代码,但我手头没有Rest的工具包,所以没有用以下的代码。
//var client = new RestClient("http://a.apix.cn/apixlife/weather/weather?cityid=your_value");
//var request = new RestRequest(Method.GET);
//request.AddHeader("apix-key", "1fa54fddcb5d4f777a76949f2596574e");
//request.AddHeader("content-type", "application/json");
//request.AddHeader("accept", "application/json");
//IRestResponse response = client.Execute(request);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
2.program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace 天气查询
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
3.Form1.Designer.cs(可视化界面自动生成的代码)
namespace 天气查询
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(63, 23);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(143, 23);
this.button1.TabIndex = 0;
this.button1.Text = "获取福州天气";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(63, 52);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(472, 226);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(632, 351);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
}
}