接口开发
来源:互联网 发布:詹姆斯和科比知乎 编辑:程序博客网 时间:2024/05/16 12:03
接口开发最基本的是如何获取请求的参数和如何返回查到的数据,参数的格式采用JSON
以struts框架为例
post方式传递json数据参数
public class GetbusinessInformation extends BaseAction{public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{JSONObject jsonRequestParam = JSONObject.fromObject(this.getStrResponse(request));JSONObject json = new JSONObject();//返回的JSON数据String requst_code = (String)jsonRequestParam.get("req_code");//请求接口路由识别码 System.out.println(jsonRequestParam.toString());//打印请求参数 if(StringUtils.equals("001", requst_code)){ json = ...................//此处调用具体的实现方法 } if(StringUtils.equals("002", requst_code)){ json = ...................//此处调用具体的实现方法 } ........................... System.out.println(json.toString());//打印输出参数 response.setCharacterEncoding("UTF-8"); response.getWriter().write(json.toString()); response.getWriter().flush(); response.getWriter().close(); return null; }}
//获取请求体中的数据 public String getStrResponse(HttpServletRequest request) throws IOException{ int contentLength = request.getContentLength(); if(contentLength<0){ return null; }byte buffer[] = new byte[contentLength];for (int i = 0; i < contentLength;) {int len = request.getInputStream().read(buffer, i, contentLength - i);if (len == -1) {break;}i += len; }return new String(buffer, "utf-8"); }
get方式传递参数,可直接用request.getParameter("参数名")获取。
阅读全文
0 0
- 接口开发
- 接口开发
- 接口开发
- 接口开发
- web开发接口,音乐接口,天气接口,地图接口
- 开发日记:接口开发设计
- SSO接口开发-FAQ
- .net开发Provision接口
- 贝贝短信开发接口
- 短消息开发接口!!!!!!!!!!!!!!!!!!
- MSN开发接口
- Google应用程序开发接口
- 支付宝接口开发
- IsmpSp接口开发
- PHP Paypal接口开发
- ucenter接口开发
- paypal支付开发接口
- hibernate 核心开发接口
- 网页弹框插件bootbox
- iOS 11 与 iPhone X的适配那些事
- AT+CNMI的使用
- h5常见问题
- 【Codeforces257E】Greedy Elevator
- 接口开发
- Java学习笔记(30)--抽象类 详解
- 表单重复提交
- Ajax原生与jQuery
- Ubuntu16安装nginx
- makefile双冒号规则
- leetcode 275. H-Index II
- Ubuntu grub recuse> 修复方法
- 36、java常用类-StringBuffer类和StringBuilder