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