如何使用JAVA接口传递参数
本篇内容介绍了“如何使用JAVA接口传递参数”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联长期为上1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为江岸企业提供专业的网站设计、成都网站建设,江岸网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
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接口传递参数”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
新闻名称:如何使用JAVA接口传递参数
转载来源:http://ybzwz.com/article/pjphhd.html