您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“如何使用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。