First, post method of passing value
public static string SENDULR (String requesturl,string xml) {string responsestr= ""; try {URL url = new URL (requesturl); HttpURLConnection URLConnection = (httpurlconnection) url.openconnection (); Urlconnection.setdooutput (TRUE); Urlconnection.setdoinput (TRUE); Urlconnection.setrequestmethod ("POST"); Urlconnection.setusecaches (FALSE); Urlconnection.setinstancefollowredirects (TRUE); Urlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded"); Urlconnection.setrequestproperty ("Charset", "UTF-8"); Urlconnection.connect (); DataOutputStream out = new DataOutputStream (Urlconnection.getoutputstream ()); String params= XML; Out.write (Params.getbytes ("UTF-8")); /* Out.writebytes (params); */Out.flush (); Out.close (); BufferedReader reader = new BufferedReader (New InputStreamReader (UrlconneCtion.getinputstream (), "UTF-8")); String Line; while (line = Reader.readline ()) = null) {responsestr+=line; } reader.close (); Urlconnection.disconnect (); }catch (malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (Protocolexception e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); } return RESPONSESTR; }
Second, interface access, return JSON object
ps://{"Stu": "One", "Stuname": "W", "Sex": "male"}string url = "Http://localhost:8080/yikang/q/yikang/YtxHaixun/test"; String par = "a=" + "123"; String str = YIKANGUTIL.SENDULR (URL, par); Jsonobject JSONP = Json.parseobject (str); System.out.println (Jsonp.get ("id"));
Third, interface access, return JSON collection
ps:[{"Stu": "One", "Stuname": "W", "Sex": "male", {"Stu": "One", " Stuname": "W", "Sex", "Male"}, {"Stu": "One", "Stuname ":" W "," Sex ":" male "}]string url =" Http://localhost:8080/yikang/q/yikang/YtxHaixun/test "; String par = "a=" + "123"; String str = YIKANGUTIL.SENDULR (URL, par); Jsonarray Parsearray = Jsonarray.parsearray (str); int size = Parsearray.size (); for (int i=0;i<size;i++) {String id = (St Ring) Parsearray.getjsonobject (i). Get ("id"); System.out.println ("= = =" +id);}
JSON pass-through value