您好,登錄后才能下訂單哦!
本篇內容介紹了“如何使用JAVA接口傳遞參數”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1,給接口傳遞json格式的數據
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請求 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(); //讀取響應 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.從接口獲取json格式數據
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.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; 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 { //實例一個url和URLConnection URL oracle = new URL(url); //打開鏈接 URLConnection yc = oracle.openConnection(); //輸入流作參數傳進InputStreamReader并用BufferedReader接受 BufferedReader in = new BufferedReader(new InputStreamReader( yc.getInputStream())); String inputLine = null; //一直讀到空,并設置流編碼是UTF8 while ( (inputLine = in.readLine()) != null) { json.append(new String(inputLine.getBytes(),"GBK")); } //記得關閉連接 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(); i++) { 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("數據大小:"+jn.size()); } catch (Exception e) { e.printStackTrace(); System.out.println("連接超時!"); } } }
“如何使用JAVA接口傳遞參數”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。