String url = "Http://127.0.0.1:8080/api"; //then get company information based on the table nameHttpPost HttpPost =Newhttppost (URL); List<NameValuePair> params =NewArraylist<namevaluepair>(); Params.add (NewBasicnamevaluepair ("Comname", Comname)); HttpResponse HttpResponse=NULL; Httpentity httpentity=NULL; Try {
Set the timeout time httpclient4.5 to build the Requestconfig object Requestconfig requestconfig=Requestconfig.custom (). Setconnecttimeout ((Setconnectionrequesttimeout). (1000). SetSocketTimeout (5000). build (); Httppost.setconfig (Requestconfig); Httppost.setentity (Newurlencodedformentity (params, HTTP. Utf_8)); HttpClient HttpClient=Newdefaulthttpclient (); HttpResponse=Httpclient.execute (HttpPost); Httpentity=httpresponse.getentity (); } Catch(unsupportedencodingexception E1) {Logger.error (E1.getmessage ()); } Catch(clientprotocolexception E1) {Logger.error (E1.getmessage ()); } Catch(IOException E1) {Logger.error (E1.getmessage ()); } if(Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Request OK Try{String result=entityutils.tostring (httpentity); Jsonobject Data=Jsonobject.fromobject (httpentity); if("$". Equals (Data.get ("code"))) {System.out.println ("Call succeeded"); }Else{logger.error (Data.get ("Message")); } } Catch(Exception e) {logger.error (E.getmessage ()); } }Else { Try{logger.error (entityutils.tostring (httpentity)); } Catch(ParseException e) {logger.error (E.getmessage ()); } Catch(IOException e) {logger.error (E.getmessage ()); } }
HttpClient Interface Call