Get the App
SLTechnology News&Howtos  ›  Development  › 

How to pass parameters using the JAVA interface

Shulou Source: shulou.com Published: 2022-06-03 06:37:58 10月06日 Update

This article introduces the knowledge of "how to use JAVA interface to pass parameters". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Pass data in json format to the interface

Import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import net.sf.json.JSONObject;public 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 DataOutputStream out = new DataOutputStream (connection.getOutputStream ()); JSONObject obj = new JSONObject () String message = java.net.URLEncoder.encode ("", "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 ();}}

two。 Get data in json format from the interface

Import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.sql.Timestimport java.util.concurrent.ConcurrentHashMap;import net.sf.json.JSONArray;import net.sf.json.JSONException;import net.sf.json.JSONObject;import org.springframework.web.HttpRequestHandler Public 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 a url and URLConnection URL oracle = new URL (url); / / Open the link URLConnection yc = oracle.openConnection () / / input stream as parameters into InputStreamReader and accept BufferedReader in = new BufferedReader (new InputStreamReader (yc.getInputStream () with BufferedReader; String inputLine = null / / read until empty, and set the stream encoding to UTF8 while ((inputLine = in.readLine ())! = null) {json.append (new String (inputLine.getBytes (), "GBK"));} / remember to close the connection in.close () } catch (Exception e) {e.printStackTrace ();} try {JSONArray jn = JSONArray.fromObject (json.toString ()); if (jn.size () > 0) {for (int I = 0; I < jn.size (); iTunes +) {JSONObject jo = (JSONObject) jn.get (I) System.out.println (jo.get ("id")); System.out.println (jo.get ("fdName"));} System.out.println (jn);} System.out.println ("data size:" + jn.size ());} catch (Exception e) {e.printStackTrace () System.out.println ("connection timeout!") That's all for "how to use the JAVA interface to pass parameters". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Interface parameters data content more format knowledge utf-8 practical learned next dilemma size example reality situation article case tape coding Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker Shulou Information Microsoft NVidia