Java interface pass-through parameter (POST), receive data from interface (JSON)-----Record

Source: Internet
Author: User

1. Pass JSON-formatted data to the interface

Import Java, I-. BufferedReader; Import Java, I-. DataOutputStream; Import Java, I-. IOException; Import Java, I-. InputStreamReader; Import Java, I-. Unsupportedencodingexception; Import Java. Net. HttpURLConnection; Import Java. Net. Malformedurlexception; Import Java. Net. Url; Import Net. SF. JSON. Jsonobject;p Ublic class Appaddtest {public static final String Add_url ="Http://192.168.1.1:8080/*.controller/*.action"; public static void Appadd () {try {URL url = new URL (add_url); HttpURLConnection connection = (httpurlconnection) URL. OpenConnection (); Connection. Setdooutput (True); Connection. Setdoinput (True); Connection. Setrequestmethod ("POST"); Connection. Setusecaches (False); Connection. Setinstancefollowredirects (True); Connection. Setrequestproperty ("Connection","Keep-alive"); Connection. Setrequestproperty ("Content-type","Text/plain; Charset=utf-8 "); Connection. Connect (); Post Request DataOutputStreamout = new DataOutputStream (connection. Getoutputstream ()); Jsonobject obj = new Jsonobject (); String message = Java. Net. Urlencoder. Encode ("Hahaha","Utf-8"); Obj. Element ("Detail","DF"); Obj. Element ("TEXT1","ASD"); Obj. Element ("TEXT2", message);Out. Writebytes ("Data=" +obj. toString ()); System. Out. println ("Data=" +obj. toString ());Out. Flush ();Out. Close (); Read response BufferedReader reader = new BufferedReader (New InputStreamReader (connection. getInputStream ())); String lines; StringBuffer sb = new StringBuffer (""); while (lines = reader. ReadLine ()) = null) {lines = new String (lines. GetBytes (),"Utf-8") ; SB. Append (lines);} System. Out. println (SB); reader. Close (); connection. Disconnect ();} catch ( Malformedurlexception e) {E.printstacktrace ()} catch (Unsupportedencodingexception e) {e. Printstacktrace ();} catch (IOException e) {e. Printstacktrace ();}} public static void Main (string[] args ) {Appadd ();}}                 

2. Get JSON-formatted data from an interface

Import Java, I-. BufferedReader; Import Java, I-. InputStreamReader; Import Java, I-. PrintWriter; Import Java. Net. Url; Import Java. Net. URLConnection; Import Java. sql. Timestamp; Import Java. util. concurrent. Concurrenthashmap; Import Net. SF. JSON. Jsonarray; Import Net. SF. JSON. Jsonexception; Import Net. SF. JSON. Jsonobject; Import org. springframework. Web. Httprequesthandler;p Ublic class Javaurl {/** * @param args */public static void main (string[] args) {String URL ="Http://192.168.1.1:8080/*.controller.do/*.action"; System. Out. println ("URL:" +url); StringBuffer json = new StringBuffer (); try {//instance one URL and urlconnection url Oracle = new URL (URL); Open link URLConnection yc = Oracle. OpenConnection (); Input stream is passed into InputStreamReader and accepted by BufferedReader BufferedReaderin = new BufferedReader (New InputStreamReader (YC. getInputStream ())); String inputline = null; Always read to null and set the stream encoding to be UTF8 while (Inputline =Inch. ReadLine ())! = NULL) {JSON. Append (New String (inputline. GetBytes (),"GBK")); }//Remember to close the connectionInch. Close (); } catch (Exception e) {E. Printstacktrace (); } try {Jsonarray jn = Jsonarray. Fromobject (JSON. toString ()); if (Jn. Size () >0) {for (int i =0; I < jn.size (); i++) {Jsonobject Jo = (jsonobject) Jn. get (i); System. Out. println (Jo.get ( "id")) ; System.out.println (Jo .get ( "Fdname")) ;} System.out.println (JN) .out.println ( " Data size: "+jn.size ()) .printstacktrace () ; System.out.println ( " Connection timed out! ")              

Java interface Pass-through parameters (POST), receiving data (JSON) from an interface-----records

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.