java获取Json和http状态码

摘要:
最近,再次进行了接口自动化测试。有几个重要的方法。1.获取http状态代码/**返回接口状态代码**/publicstaticStringgetHttpCode(Stringurl){Stringcode=null;try{URLu=newURL(url);URLConnectionuc=u.openConnection();HttpURLConnectionhuc=(HttpURLConnection)

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

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状态码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mysql 动态拼接表字段,值 mybatis 动态获取表字段VMware共享文件夹下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【转】sql 中把datetime转换成string 函数

http://blog.knowsky.com/186776.htm http://www.cnblogs.com/fanadu/archive/2009/01/05/1368825.html 0 Feb 22 2006 4:26PM  CONVERT(CHAR(19), CURRENT_TIMESTAMP, 0) 1  02/22/06  CON...

使用Mybatis执行sql脚本

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...

MP实战系列(十二)之封装方法详解(续二)

继续MP实战系列(十一)之封装方法详解(续一)这篇文章之后。 此次要讲的是关于查询。 查询是用的比较多的,查询很重要,好的查询,加上索引如鱼得水,不好的查询加再多索引也是无济于事。 1.selectById()方法 演示示例: UserEntity user = ud.selectById(33); System.out....

Assert中的静态方法

junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类。 1.assertTrue/False([String message,]boolean condition);    用来查看变量是是否为false或true,如果assertFalse()查看的变量的值是false则测试成功,如果是true则失败,as...

1034 Head of a Gang (30 分)(图的遍历or并查集)

dfs #include<bits/stdc++.h> using namespace std; const int N=3000; int mp[N][N]; int weight[N]; int vis[N]; map<string,int>si; map<int,string>is; map<string...

django实现支付宝支付

目录 django支付宝支付 新建支付宝应用 创建应用(使用沙箱环境测试) 按照官方要求生成私钥(可以上支付宝开发平台下载支付宝开发助手) 把生成的app公钥粘贴到沙箱的app中 查看沙箱账号和密码 支付宝开发地址 说明 在utils中封装请求支付宝扫码地址url的函数和生成订单id的函数 在model.py中定义表 在views.py中...