Recently we have done interface Automation test, and several methods are more important
1. Get the HTTP status code
/** Return Interface status code **/ Public Staticstring gethttpcode (string url) {String code=NULL; Try{URL u=Newurl (URL); URLConnection UC=u.openconnection (); HttpURLConnection Huc=(httpurlconnection) UC; Code=NewInteger (Huc.getresponsecode ()). ToString (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnCode; }
2. Get JSON
/** 3 Parameters **/ Public Staticstring 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= "The status code for the ******* interface is:" +code+ "*******" +URL; }Else{StringBuilder JSON=NewStringBuilder (); Try{URL u=Newurl (URL); URLConnection UC=u.openconnection (); BufferedReader BD=NewBufferedReader (NewInputStreamReader (Uc.getinputstream (), "UTF-8")); String s=NULL; while((S=bd.readline ())! =NULL) {json.append (s); } bd.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } result=json.tostring (); } returnresult; }
3. Get a value in JSON, such as {"A", "1"}
Public Staticstring Getjsonvalue (String json, string name) {string S=NULL; Jsonobject Jobj=Jsonobject.fromobject (JSON); //Jsonobject jobj = Jsonobject.fromobject ("{' Total ': ' 0 ', ' message ': ' User number cannot be null ', ' 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 (); } } returns; }
4. Get a value for the second-dimension JSON in double json, such as {"A", "1", {"B", "1"}
Public Static string Getjsonvalue (String json,string jdate, string name) { = jsonobject.fromobject (JSON); Jsonobject dataList=jobj.getjsonobject (jdate); =datalist.getstring (name); return balance;
5. Get the JSON containing the nth JSON in the array, such as {"A", "1", "B", "[{" A2 "," 1 "},{" B2 "," 1 "}]"}
/* Get Jsonarray */ Public Static int index) { = jsonobject.fromobject (JSON); Jsonarray Childs= Jobj.getjsonarray (arr_name); = Childs.getjsonobject (index); return job.tostring (); }
6. Connect to the database
Public StaticList<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 =NewArraylist<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 blockE.printstacktrace (); } finally { Try{rs.close (); Con.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnlist; }
Java gets JSON and HTTP status codes