In the Java C/S software development, docking a number of third-party provided web interface.
The way you use the interface is not as difficult as you might think.
For example, if the provided interface is a get form, the address is output directly on the Web page, and the parameters are pieced together to get the return value.
If it is a post interface, it is necessary to make a request for an interface, such as postman or the browser's own plug-in, and so on.
The same is true for code conversion.
Here is an example of a simple post interface invocation:
The P:param parameter can be used to pass in a Jsonobject.fromobject (object)
/** * @author: CJD * @description: Post interface returns result String * @params: [URL, param] * @param URL Request interface * @param param JSON string required * @return: java.lang.String * @date: 17:31 2018/8/1 */public static string Sendpo St (string URL, string param) {outputstreamwriter out = null; BufferedReader in = null; String result = ""; try {URL realurl = new URL (URL); HttpURLConnection conn = null; Connection between open and URL conn = (httpurlconnection) realurl.openconnection (); The Send POST request must be set to the following two lines conn.setdooutput (true); Conn.setdoinput (TRUE); Conn.setrequestmethod ("POST"); Post method//Set general request attribute Conn.setrequestproperty ("accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); Conn.setrEquestproperty ("Content-type", "application/json;charset=utf-8"); Conn.connect (); Gets the output stream corresponding to the URLConnection object out = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8"); Send request parameter out.write (param); Flush output Stream Buffer Out.flush (); Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ())); String Line; while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {System.out.println ("send POST request exception! "+ e); E.printstacktrace (); }//Use finally block to close the output stream, input stream finally {try {if (out! = null) {out . Close (); } if (in = null) {in.close (); }} catch (IOException ex) {ex.printstacktrace (); } } return result; }
When the method is called, a string format for the returned JSON data is obtained.
Java is an object-oriented language, so using Jsonobject/jsonarray to convert the JSON data into an object of the entity class, for example I use, because the interface returned there is a bit strange, so I took a few steps to deal with the following:
Prepickupmailres Resultbean = new Prepickupmailres (); Jsonobject Respjson = Jsonobject.fromobject (Jsonobject.fromobject (JSON). Get ("Respjson")); Jsonarray Jsonarray = (jsonarray) respjson.get ("Prepickuprspdetaildtolist"); Jsonobject Resultjson = (jsonobject) jsonarray.get (0); Jsonutils.getmorpherregistry (). Registermorpher (New Datemorpher (New string[]{"Mm/dd/yyyy HH:mm:ss"}); Resultbean = (prepickupmailres) Jsonobject.tobean (Resultjson, Prepickupmailres.class);
So the last thing we get is an entity object.
For more information on how to use Jsonobject/jsonarray, see:
https://blog.csdn.net/chijiandi/article/details/81011369
C/S software development of Java, docking some third-party provided web interface. The way the
interfaces are used is not as difficult as it might seem.
For example, if the provided interface is a get form, the address is output directly on the Web page, and the parameters are pieced together to get the return value.
If it is a post interface, it is necessary to make a request for an interface, such as postman or browser-brought plug-ins, and so on. The
translates into code as well.
The following is a simple example of a post interface invocation:
The P:param parameter can be passed in with Jsonobject.fromobject (object) to the
/** * @author: CJD * @description: Post interface returns result String * @params: [URL, param] * @param URL Request interface * @param param JSON string required * @return: java.lang.String * @date: 17:31 2018/8/1 */public static string Sendpo St (string URL, string param) {outputstreamwriter out = null; BufferedReader in = null; String result = ""; try {URL realurl = new URL (URL); HttpURLConnection conn = null; Connection between open and URL conn = (httpurlconnection) realurl.openconnection (); The Send POST request must be set to the following two lines conn.setdooutput (true); Conn.setdoinput (TRUE); Conn.setrequestmethod ("POST"); Post method//Set general request attribute Conn.setrequestproperty ("accept", "*/*"); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); Conn.setrEquestproperty ("Content-type", "application/json;charset=utf-8"); Conn.connect (); Gets the output stream corresponding to the URLConnection object out = new OutputStreamWriter (Conn.getoutputstream (), "UTF-8"); Send request parameter out.write (param); Flush output Stream Buffer Out.flush (); Defines the BufferedReader input stream to read the response of the URL in = new BufferedReader (New InputStreamReader (Conn.getinputstream ())); String Line; while (line = In.readline ()) = null) {result + = line; }} catch (Exception e) {System.out.println ("send POST request exception! "+ e); E.printstacktrace (); }//Use finally block to close the output stream, input stream finally {try {if (out! = null) {out . Close (); } if (in = null) {in.close (); }} catch (IOException ex) {ex.printstacktrace (); } } return result; }
When the method is called, a string format for the returned JSON data is obtained.
Java is an object-oriented language, so using Jsonobject/jsonarray to convert the JSON data into an object of the entity class, for example I use, because the interface returned there is a bit strange, so I took a few steps to deal with the following:
Prepickupmailres Resultbean = new Prepickupmailres (); Jsonobject Respjson = Jsonobject.fromobject (Jsonobject.fromobject (JSON). Get ("Respjson")); Jsonarray Jsonarray = (jsonarray) respjson.get ("Prepickuprspdetaildtolist"); Jsonobject Resultjson = (jsonobject) jsonarray.get (0); Jsonutils.getmorpherregistry (). Registermorpher (New Datemorpher (New string[]{"Mm/dd/yyyy HH:mm:ss"}); Resultbean = (prepickupmailres) Jsonobject.tobean (Resultjson, Prepickupmailres.class);
So the last thing we get is an entity object.
Related articles:
Example of a call method in a C # interface in a derived class and an external class
Want to make a PHP call Java WebService Interface example