最近再做接口自动化测试,其中有几个方法比较重要

1.获取http状态码

        /*
* 返回接口状态码
* */
public static String getHttpCode(String url) {
String code = null;
try {
URL u = new URL(url);
URLConnection uc = u.openConnection();
HttpURLConnection huc = (HttpURLConnection)uc;
code = new Integer(huc.getResponseCode()).toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return code;
}

2.获取json

        /*
* 3个参数
* */
public static String getJson(String base_url, String para1, String value1, String para2, String value2, String para3, String value3) {
String url = base_url + para1 + "=" + value1 + "&"
+ para2 + "=" + value2 + "&"
+ para3 + "=" + value3; String result = ""; String code = getHttpCode(url);
if(!code.startsWith("2")) {
result = "*******接口的状态码为:"+code+"*******"+url;
}else {
StringBuilder json = new StringBuilder();
try {
URL u = new URL(url);
URLConnection uc = u.openConnection();
BufferedReader bd = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
String s = null;
while((s=bd.readLine())!=null) {
json.append(s);
}
bd.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result = json.toString();
}
return result;
}

3.获取json中的某个值,如{"a":"1"}

        public static String getJsonValue(String json, String name){

            String s = null;
JSONObject jobj = JSONObject.fromObject(json);
//JSONObject jobj = JSONObject.fromObject("{'total':'0','message':'用户编号不能为空','data':'','code':'2','rows':''}");
Iterator it = jobj.keys();
while(it.hasNext()){
String key = it.next().toString();
String value = jobj.getString(key);
if(key.equals(name)) {
s = value.trim();
}
}
return s; }

4.获取双重json中第二维json的某个值,如{"a":"1",{"b":"1"}}

        public static String getJsonValue(String json,String jdate, String name){

            JSONObject jobj = JSONObject.fromObject(json);
JSONObject dataList=jobj.getJSONObject(jdate);
String balance =dataList.getString(name);
return balance;
}

5.获取json中包含数组中的第N个json,如{"a":"1","b":"[{"a2":"1"},{"b2":"1"}]"}

    /*获取jsonArray*/
public static String getJsonArray(String json, String arr_name, int index) { JSONObject jobj = JSONObject.fromObject(json);
JSONArray childs= jobj.getJSONArray(arr_name);
JSONObject job = childs.getJSONObject(index); return job.toString();
}

XIAOBAI

static void getVo(){
String result = HttpClientUtil.httpGet("http://127.0.0.1:8080/test/vo?id=100");
JSONObject jsonObject = JSONObject.parseObject(result);
JSONObject userObject = jsonObject.getJSONObject("user");
Object object = userObject.get("mobile");
System.out.println("mobile="+object);
//获取json中message的string值
String message = jsonObject.getString("message");
System.out.println(message);
//获取json中code的int值
int code = jsonObject.getIntValue("code");
System.out.println(code); //获取json数组
JSONArray jsonArray = jsonObject.getJSONArray("users");
JSONObject userJson = jsonArray.getJSONObject(1);
System.out.println(userJson.get("id")); }

6.连接数据库

        public static List<String> connectSqlList(String sql,String userNum, String col) {
String url = "jdbc:mysql://172.16.30.209:3306/a_test";
String name = "gmsd";
Connection con = null;
ResultSet rs = null;
String rssql = null; List<String> list = new ArrayList<String>(); try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,name,"dlnu1234");
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, userNum); rs = pst.executeQuery();
while(rs.next()) {
rssql = rs.getString(col);
list.add(rssql);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list; }

java获取Json和http状态码的更多相关文章

  1. 转载: java获取json数组格式中的值

    转自:https://www.cnblogs.com/kkxwze/p/11134846.html   第一种方法: String str = "{'array':[{'id':5,'nam ...

  2. java获取json数组格式中的值

    第一种方法: String str = "{'array':[{'id':5,'name':'张三'},{'id':6,'name':'李四'}]}"; JSONArray jso ...

  3. python(30) 获取网页返回的状态码,状态码对应问题查询

    获取访问网页返回的状态码 html = requests.get(Url) respon = html.status_code 以下内容来自于维基百科:点击查看网页 1xx消息 这一类型的状态码,代表 ...

  4. curl获取http请求的状态码

    $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //设置头文件的信息作为数据流输出 curl_setopt($curl, CUR ...

  5. PHP获取访问页面HTTP状态码的实现代码

    方法一 $header_info=get_headers('//www.jb51.net/'); echo $header_info[0]; //耗时 0.67~0.78 方法二 $ch = curl ...

  6. Java获取汉字的大小写拼音码(汉字的拼音首字母)

    import java.io.UnsupportedEncodingException; /** * 获取拼音码 * * @author xmj * */ public class GetPinyin ...

  7. java 获取json字符串中key对应的值

    用到了Gson的JsonParser maven项目引入 <dependency> <groupId>com.google.code.gson</groupId> ...

  8. 【Java】java获取json中某个字段

    import com.alibaba.fastjson.JSONObject; public class JsonTest { public static void main(String[] arg ...

  9. java获取json格式中的值

    先右键项目,然后点击properties,然后选中java Builder Path,选择add external jars,引入需要引入json.jar package web; import or ...

随机推荐

  1. C#开发规范总结(个人建议)

    .NET开发编程规范 章程序的版式 版式虽然不会影响程序的功能,但会影响可读性.程序的版式追求清晰.美观,是程序风格的重要构成因素. 可以把程序的版式比喻为"书法".好的" ...

  2. 我见过的几门语言中的hello world

    1.Java public class hello { public static void main(String[] args){ System.out.println("hello w ...

  3. 我的Machine Learning学习之路

    从2016年年初,开始用python写一个简单的爬虫,帮我收集一些数据. 6月份,开始学习Machine Learning的相关知识. 9月开始学习Spark和Scala. 现在想,整理一下思路. 先 ...

  4. sphinx使用随笔

    为什么需要进行全文搜索呢? 一个表中有a.b.c多个字段.我们使用sql进行like搜索的时候,往往只能匹配某个字段.或者是这样的形式:a LIKE “%关键词%”or b LIKE “关键词” 这样 ...

  5. webservice 的wsdl文件生成客户端java类

    提供两个方法: 第一个: 发布webservice项目后, 地址栏地址  http://localhost:8888/lxitedu.webservice.cxf-ch2/services/userS ...

  6. 用QQ号登陆Sharepoint,研究到最后关头卡住了。大家发力呀

    此项目未完成,登陆不了SharePoint,大家研究吧,折腾吧..... 已经完成的部分有:已经可以获取到腾讯用户信息,如: Get Access Token===============access ...

  7. Upgrade custom workflow in SharePoint

    Experience comes when you give a try or do something, I worked in to many SharePoint development pro ...

  8. Retrofit源码设计模式解析(下)

    本文将接着<Retrofit源码设计模式解析(上)>,继续分享以下设计模式在Retrofit中的应用: 适配器模式 策略模式 观察者模式 单例模式 原型模式 享元模式 一.适配器模式 在上 ...

  9. HTML列表元素

    HTML定义了3类列表: 1.有序列表(通常用数字编号) 2.无序列表(通常前面加原点) 3.自定义列表(列表项目,带有集成的定义) 有序列表和无序列表均为列表中的每一项使用列表项元素(<li& ...

  10. C语言-04-函数

    函数 函数是一组一起执行任务的语句,函数是一个可执行C程序必不可少的条件(至少一个main()函数),函数的定义形式 returnType functionName() { bodyOf of the ...