zoukankan      html  css  js  c++  java
  • java获取Json和http状态码

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

    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;
                 
            }
  • 相关阅读:
    vsftpd被动模式配置
    CentOS 6.4 服务器版安装教程(超级详细图解)
    CentOS 5.4 安装和卸载桌面
    CentOS 删除桌面环境
    centos x64 vsftpd 530登陆错误问题
    [CentOS] CentOS for vsftpd with MySQL Virtual user
    vsftp 无法启动,500 OOPS: bad bool value in config file for: anonymous_enable
    第二章 创建对话框
    error: no matching function for call to 'Ui::GoToCellDialog::setupUi(QDialog*&)' ui.setupUi(dialog); ^
    第一章 Qt入门
  • 原文地址:https://www.cnblogs.com/qiaoyeye/p/4730930.html
Copyright © 2011-2022 走看看