|
接口的理解
接口就是API,可以理解为用来供外部调用(访问)的东东(模块)。
比如:
手机上可以查看实时天气(大概原理就是app应用调用了气象平台提供天气查询接口);12306网站的实名制认证
(需要调用政府公安局相关信息系统提供的身份认证接口);
微信上绑定银行卡(需要调用银行系统提供的相关业务接口)。
JSON介绍
JSON是一种数据格式,任何对象都可以使用JSON数据来表示。
可以表示某人,比如:
{
"name":"Bill Gates",
"street":"Fifth Avenue New York 666",
"age":56,
"phone":"5551234567"
}
可以表示网站,比如:
{ "sites": [ { "name":"菜鸟教程" , "url":"www.runoob.com" }, { "name":"google" , "url":"www.google.com" }, {
"name":"微博" , "url":"www.weibo.com" } ] }
等等。
注意:还有另外一种数据格式叫XML。
Web接口的访问
比如:
天气查看接口的地址为:http://weather.51wnl.com/weatherinfo/GetMoreWeather
参数列表如下:
参数名 备注
cityCode 城市编码
weatherType 查看类型[0:查看一周内的天气情况 1:查看当天的天气情况]
打开浏览器,输入如下地址:
返回的json数据如下:
如果要格式化显示json数据的话,可以在火狐上安装格式化json数据的插件
或者,通过在线网站对json数据格式化显示
Web接口自动化测试
检查接口返回的城市和温度是否正确。
代码如下:
- package day01;
- import static org.junit.Assert.*;
- import java.util.Arrays;
- import java.util.List;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONObject;
- import org.junit.After;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized.Parameters;
- import org.junit.runners.Parameterized;
- @RunWith(Parameterized.class)
- public class WeatherTest {
- private CloseableHttpClient httpClient;
- private int id;//用例编号
- private String city;//城市编号
- private int type;//查看类型 0:查看本周内的天气 1:查看当天的天气
- private String expectedCity;//期望值
- private String expectedTemp;//期望温度
- public WeatherTest(int id, String city, int type, String expectedCity, StringexpectedTemp) {
- super();
- this.id = id;
- this.city = city;
- this.type = type;
- this.expectedCity = expectedCity;
- this.expectedTemp = expectedTemp;
- }
- @After
- public void end() throws Exception{
- httpClient.close();//关闭客户端
- }
- @Parameters
- public static List data(){
- Object[][] arr = {
- {1,"101010100",1,"北京","-1"},
- {2,"101020100",1,"上海","5"},
- {3,"101280101",1,"广州","8"}
- };
- return Arrays.asList(arr);
- }
- @Test
- public void test() throws Exception{
- //接口地址
- String url ="http://weather.51wnl.com/weatherinfo/GetMoreWeather";
- //创建一个客户端用来访问接口
- httpClient= HttpClients.createDefault();
- //HttpClient httpClient = new DefaultHttpClient();
- //创建一个请求
- HttpGet httpGet =new HttpGet(url+"?cityCode="+city+"&weatherType="+type);
- //发送该请求
- HttpResponse result =httpClient.execute(httpGet);
- //获取接口返回的内容
- String resData= EntityUtils.toString(result.getEntity());
- //将接口返回的内容转成JSON实例
- JSONObject json =new JSONObject(resData);
- //从JSON实例里面获取天气实例
- JSONObject weatherInfo =json.getJSONObject("weatherinfo");
- //验证返回的城市是否和期望值相等
- assertEquals(expectedCity,weatherInfo.getString("city"));
- //验证返回的温度是否和期望值相等
- assertEquals(expectedTemp,weatherInfo.getString("temp"));
- }
- }
复制代码
附截图:
在HRIS系统中调用天气查看接口(了解)
步骤一、引入相关的jar包
步骤二、创建一个Servlet
该Servlet会调用天气查询的接口,代码如下:
- package com.hris.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- public class WeatherServlet extends HttpServlet {
- public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
- String url ="http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode="+request.getParameter("city")+"&weatherType=1";
- try{
- CloseableHttpClient httpClient = HttpClients.createDefault();
- HttpGet httpGet =new HttpGet(url);
- HttpResponse result =httpClient.execute(httpGet);
- String resData = EntityUtils.toString(result.getEntity());
- response.setHeader("Content-type","application/json;charset=UTF-8");
- response.setCharacterEncoding("utf-8");
- response.setHeader("Cache-Control","no-cache");
- PrintWriter printWriter =response.getWriter();
- printWriter.write(resData);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
复制代码
附截图:
步骤三、修改web.xml配置
添加如下配置:
- <servlet>
- <servlet-name>weather</servlet-name>
- <servlet-class>com.hris.servlet.WeatherServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>weather</servlet-name>
- <url-pattern>/getWeatherInfo</url-pattern>
- </servlet-mapping>
复制代码
附截图:
步骤四、login.jsp页面修改HTML代码
在导航层里面新增如下HTML代码:
城市:<select id="city" name="city">
<option value="101010100">北京</option>
<option value="101020100" selected>上海</option>
<option value="101280101">广州</option>
</select>
<span id="tianqi"></span>
body开始标签里面的html更改为如下:
<body>
附截图:
步骤五、login.jsp页面增加JS程序发送AJAX请求查询天气
添加如下代码:
- <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("#city").change(function(){
- $.post("getWeatherInfo",
- {
- city:$(this).val()
- },
- function(data,status){
- //var obj = $.parseJSON(data);
- $("#tianqi").html("气温:"+data.weatherinfo.temp+"°C");
- });
- });
- });
- </script>
- <script type="text/javascript">
- function method(){
- $.post("getWeatherInfo",
- {
- city:$("#city").val()
- },
- function(data,status){
- //var obj = $.parseJSON(data);
- $("#tianqi").html("气温:"+data.weatherinfo.temp+"°C");
- });
- }
- </script>
复制代码
附截图效果:
|
|