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; }
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************