Java
The development interface transmits data using the HTTP protocol
This interface is mainly used to log in, the Java server provides an interface, mobile device clients (Android and iOS) through this interface to the user name and password and other things to the server authentication, The server then returns data telling the client whether the login was successful. For example 0 is success, 1 is failure. The transfer of intermediate data is done through the HTTP protocol. How do I write this interface?
Code 1.2 is the server interface, which receives the client's information parsing validation and returns the required information.
Service-side code:
1 Public classTesttransfersextendsHttpServlet {2 3 Private Static Final LongSerialversionuid = 1L;4 5 //HttpServletRequest and HttpServletResponse are used to receive requests and return interface values6 //Doget for receiving a Get method7 protected voidDoget (httpservletrequest request,httpservletresponse response)throwsservletexception, IOException {8 9 }Ten One //Dopost for receiving post methods A protected voidDoPost (httpservletrequest request,httpservletresponse response)throwsservletexception, IOException { - - //determine if the request message is from the IP address of the generation system theString IP =request.getremotehost (); - //get the received message -BufferedReader reader =Request.getreader (); - +String line = ""; - +StringBuffer inputstring =NewStringBuffer (); A at while(line = Reader.readline ())! =NULL) { - - Inputstring.append (line); - - } - //additional authentication and encryption parameters can be added to the message if necessary in //parse the obtained message, according to IP address, other authentication, encryption and so on to determine whether the server has permission to request the message - //if the request is validated, the returned message is assembled according to the requested parameters to //the message to be returned +StringBuffer Resultbuffer =NewStringBuffer (); - theResultbuffer.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); * $Resultbuffer.append ("<report_data>");Panax Notoginseng -Resultbuffer.append ("<respon_req>953947334</respon_req>"); the +Resultbuffer.append ("<respon_time>20120402113943</respon_time>"); A theResultbuffer.append ("<result>"); + -Resultbuffer.append ("<id>0000</id>"); $ $Resultbuffer.append ("<comment> success </comment>"); - -Resultbuffer.append ("</result>"); the -Resultbuffer.append ("<items>");Wuyi theResultbuffer.append ("<item>"); - WuResultbuffer.append ("<county> Changzhi County </county>"); - AboutResultbuffer.append ("<company> CRC </company>"); $ -Resultbuffer.append ("<speciality> lines </speciality>"); - -Resultbuffer.append ("<personnel> kingmoral and </personnel>"); A +Resultbuffer.append ("<begin_time>20120301000000</begin_time>"); the -Resultbuffer.append ("<end_time>20120331235959</end_time>"); $ theResultbuffer.append ("<plan_quantity>50</plan_quantity>"); the theResultbuffer.append ("<checkout_quantity>40</checkout_quantity>"); the -Resultbuffer.append ("<patrol_rate>0.80</patrol_rate>"); in theResultbuffer.append ("</item>"); the About // ...... the the // ...... the + // ...... - the //message for cyclic assembly responseBayi theResultbuffer.append ("</items>"); the -Resultbuffer.append ("</report_data>"); - the //format the sending message the theResponse.setcontenttype ("Text/xml"); the -Response.setcharacterencoding ("UTF-8"); the thePrintWriter out =Response.getwriter (); the 94 out.println (resultbuffer.tostring ()); the the Out.flush (); the 98 out.close (); About - }101 102}
Client calling Code:
1 Public voidSendMessage ()throwsException {2System.out.println ("Call servlet start =================");3StringBuffer sendstr =NewStringBuffer ();4Sendstr.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");5Sendstr.append ("<report_data>");6Sendstr.append ("<request_req>953943547334</request_req>");7Sendstr.append ("<request_time>2012040211394324</request_time>");8Sendstr.append ("<request_param>");9Sendstr.append ("<query_month>201203</query_month>");TenSendstr.append ("</request_param>"); OneSendstr.append ("</report_data>"); ABufferedReader reader =NULL; - Try { -String strmessage = ""; theStringBuffer buffer =NewStringBuffer (); - //address of the receiving message -URL Uploadservlet =NewURL ( -"Http://localhost:9090/TestTransfers"); +HttpURLConnection servletconnection =(httpurlconnection) Uploadservlet - . OpenConnection (); + //Setting Connection Parameters AServletconnection.setrequestmethod ("POST"); atServletconnection.setdooutput (true); -Servletconnection.setdoinput (true); -Servletconnection.setallowuserinteraction (true); - //open stream, write XML data -OutputStream output =Servletconnection.getoutputstream (); -SYSTEM.OUT.PRINTLN ("Sent message:"); in System.out.println (sendstr.tostring ()); - Output.write (Sendstr.tostring (). GetBytes ()); to Output.flush (); + output.close (); - //get the returned data theInputStream InputStream =Servletconnection.getinputstream (); *Reader =NewBufferedReader (NewInputStreamReader (InputStream)); $ while((strmessage = Reader.readline ())! =NULL) {Panax Notoginseng buffer.append (strmessage); - } theSYSTEM.OUT.PRINTLN ("Receive return value:" +buffer); +}Catch(java.net.ConnectException e) { A Throw NewException (); the}finally { + if(Reader! =NULL) { - reader.close (); $ } $ } - } - the
"Go-Organize" Java external Interface Development (HTTP) server-client