(JAVA+TESTNG 三)Eclipse+TestNG搭建接口自动化测试框架
上一篇 /
下一篇 2017-09-04 17:24:08
转载http://www.cnblogs.com/findyou/p/5388853.html
1、接口说明
例:北京市天气
接口的址:http://www.weather.com.cn/data/cityinfo/101010100.html
- 请求方式:GET
- 请求结果:
- {
- "weatherinfo": {
- "city": "北京",
- "cityid": "101010100",
- "temp1": "15℃",
- "temp2": "5℃",
- "weather": "多云",
- "img1": "d1.gif",
- "img2": "n1.gif",
- "ptime": "08:00"
- }
- }
问题:通过浏览器用get方法访问接口地址,请求结果中文显示乱码。解决方法:firefox,查看-文字编码-Unicode
2、测试目的
请求对应cityid代码,返回的城市是否是预期城市
3、Java工程
3.1.2 新建JAVA工程
步骤同上一篇2.2.2.1新建JAVA工程,不再复述,如不懂请百度Eclipse新建工程
1.工程结构说明
2.Common.java源码
- package findyou.Interface;
- import org.codehaus.jettison.json.JSONException;
- import org.codehaus.jettison.json.JSONObject;
-
- public class Common {
-
-
-
-
-
-
-
-
- public static String getJsonValue(String JsonString, String JsonId) {
- String JsonValue = "";
- if (JsonString == null || JsonString.trim().length() < 1) {
- return null;
- }
-
- try {
- JSONObject obj1 = new JSONObject(JsonString);
- JsonValue = (String) obj1.getString(JsonId);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return JsonValue;
- }
- }
JSONObject.getString()// 根据key返回一个字符串
JSONObject.getInt()// 根据key返回一个整形数据
trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了
Android中的JSON详细总结 http://www.jb51.NET/article/33414.htm
JSONObject与JSONArray的使用http://www.cnblogs.com/xwdreamer/archive/2011/12/16/2296904.html
JSON for java入门总结 http://blog.csdn.net/xiazdong/article/details/7059573
3.getCityWeathe.java源码
- package findyou.Interface;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- public class getCityWeather {
- private String url="";
- public String geturl() {
- return url;
- }
-
- public String getHttpRespone(String cityCode) throws IOException {
- String line = "";
- String httpResults = "";
- url=("http://www.weather.com.cn/data/cityinfo/"
- + cityCode + ".html");
- try {
- HttpURLConnection connection = URLConnection
- .getConnection(url);
- DataOutputStream out = null;
-
- connection.connect();
- out = new DataOutputStream(connection.getOutputStream());
- out.flush();
- out.close();
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- while ((line = reader.readLine()) != null) {
- httpResults = httpResults + line.toString();
- }
- reader.close();
-
- connection.disconnect();
- }
收藏
举报
TAG:
标题搜索
日历
|
日 |
一 |
二 |
三 |
四 |
五 |
六 |
| | | 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 | |
数据统计
- 访问量: 3272
- 日志数: 2
- 建立时间: 2010-12-14
- 更新时间: 2017-09-04