首页 > 学院 > 开发设计 > 正文

EAS开发之接口小程序下

2019-11-06 06:18:51
字体:
来源:转载
供稿:网友

EAS开发之接口小程序下

PS:排版不好还请见谅

服务器端注意事项代码展示扩展链接

服务器端注意事项

StringB.append(SQL) SQL字符串连接 可以适当的输出字符串来看看,不要小看空格的用处,根据输出内容进行修改更加方便

格式转换 处理API返回值时经常用到格式转换 JSONObject jsondata = json.getJSONObject(“data”);取值 Integer.valueOf(this.comid.getText()) Sring转int String comid = Integer.toString(id); int转String

测试数据 测试数据时的注意事项 int id = (int) ((Math.random() * 9 + 1) * 10000); String pxbid = Integer.toString(id); 随机5位数生成 String result = “{/”err/”: 0,/”data/”:13141}”; 转义后的String仿json内容。

常用数据库技巧 添加时的一些技巧(ORA) sysdate:ORA当前系统时间 String sctx = ctx.getUserName(); 当前操作用户名代码规范 要向前台返回错误原因或错误代码 灵活的使用条件判断来确保程序正常执行。

代码展示

我会尽可能的完善注释,如有不明白或者错误的地方,请留言给我或者给我发邮件 —— [ 联系我 ]

package com.kingdee.eas.custom.pxb.app;import net.sf.json.JSONObject;import org.apache.log4j.Logger;import javax.ejb.*;import java.rmi.RemoteException;import java.sql.SQLException;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import com.kingdee.bos.*;import com.kingdee.bos.util.BOSObjectType;import com.kingdee.bos.metadata.IMetaDataPK;import com.kingdee.bos.metadata.rule.RuleExecutor;import com.kingdee.bos.metadata.MetaDataPK;//import com.kingdee.bos.metadata.entity.EntityViewInfo;import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;//import com.kingdee.bos.dao.IObjectPK;import com.kingdee.bos.dao.IObjectValue;import com.kingdee.bos.dao.IObjectCollection;import com.kingdee.bos.service.ServiceContext;import com.kingdee.bos.service.IServiceContext;import com.kingdee.jdbc.rowset.IRowSet;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PRintWriter;import java.lang.String;import java.net.URL;import java.net.URLConnection;public class PxbPreFacadeControllerBean extends AbstractPxbPreFacadeControllerBean{ private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.pxb.app.PxbPreFacadeControllerBean"); @Override protected String _preadd(Context ctx, String name, String esid) throws BOSException { // TODO Auto-generated method stub String ss = ""; String employee_id = ""; String sctx = ctx.getUserName(); Date date = new Date(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(date); // 模拟返回值 String result = "{/"err/": 0,/"data/":13141}"; // 测试数据--随机数ID int id = (int) ((Math.random() * 9 + 1) * 10000); String pxbid = Integer.toString(id); StringBuffer sql = new StringBuffer(); StringBuffer sqls = new StringBuffer(); StringBuffer sqlcom = new StringBuffer(); //关联查寻只要职位名称 sqlcom.append("select tm.FPositionID,tm.FPersonID,tt.fname_l2,tp.fname_l2,tw.FDeptID "); sqlcom.append("from T_ORG_PositionMember tm "); sqlcom.append("inner join T_BD_Person tp on tm.FPersonID = tp.fid "); sqlcom.append("inner join T_ORG_Position tt on tm.FPositionID = tt.fid "); sqlcom.append("inner join T_HR_PersonWorkExpCur tw on tw.FPositionID = tt.fid "); sqlcom.append("WHERE "); sqlcom.append("tm.FPersonID = "); sqlcom.append("'"+esid+"'"); sqlcom.append("and rownum=1"); IRowSet rss = com.kingdee.eas.util.app.DbUtil.executeQuery(ctx, sqlcom.toString()); try { while (rss.next()){ employee_id=rss.getString("FDeptID"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //插入操作sql sql.append("INSERT INTO"); sql.append(" CT_TES_pxbperson"); sql.append(" (FID,Fname_l2,FEsid,FHROrgUnitID,FBizdate,Auther)"); sql.append("VALUES"); sql.append("('" + pxbid + "','" + name + "','" + esid + "','" + employee_id + "',sysdate,'" + sctx + "')"); sqls.append("select "); sqls.append("FEsid "); sqls.append("FROM "); sqls.append("CT_TES_pxbperson "); sqls.append("WHERE "); sqls.append("FEsid = "); sqls.append("('" + esid + "')"); IRowSet rs = com.kingdee.eas.util.app.DbUtil.executeQuery(ctx, sqls.toString()); if (rs.size() != 0) { pxbid = "已存在"; } else { try { Map<String, String> mapParam = new HashMap<String, String>(); mapParam.put("organization_name", name); mapParam.put("parent_id", employee_id); String pathUrl = "http://www.91pxb.com/api/Employees/Add"; // 培训宝API // String result = PostHttp(pathUrl, mapParam); //if(判断api返回结果){ com.kingdee.eas.util.app.DbUtil.execute(ctx, sql.toString()); System.out.println("---------------插入成功-------------"); //} } catch (Exception e) { pxbid = "插入失败"; System.out.println("---------------插入失败-------------"); // TODO: handle exception } } return pxbid; } @Override protected void _precrdb(Context ctx) throws BOSException { // TODO Auto-generated method stub StringBuffer sql=new StringBuffer(); sql.append("create table CT_TES_pxbperson("); sql.append("FID varchar(40) primary key,"); sql.append("Fname_l2 varchar(40),"); sql.append("FEsid varchar(40),"); sql.append("FHROrgUnitID varchar(100),"); sql.append("auther varchar(40),"); sql.append("FbizDate datetime)"); //sql.append("PRIMARY KEY (FID) )"); com.kingdee.eas.util.app.DbUtil.execute(ctx, sql.toString()); System.out.println("---------创建成功---------"); super._precrdb(ctx); } @Override protected String _predel(Context ctx, String name) throws BOSException { // TODO Auto-generated method stub Map<String, String> mapParam = new HashMap<String, String>(); int id = (int) ((Math.random() * 9 + 1) * 10000); String pxbid = Integer.toString(id); mapParam.put("id",pxbid); String pathUrl = "http://www.91pxb.com/api/Employees/Delete"; //培训宝API //String result = PostHttp(pathUrl, mapParam); //if(判断api返回结果){ String result = "{/"err/": 0,/"data/":true}"; JSONObject json = JSONObject.fromObject(result); String data = json.getString("data"); StringBuffer sql=new StringBuffer(); sql.append("delete "); sql.append("from"); sql.append(" CT_TES_pxbperson "); sql.append("where"); sql.append(" FEsid = '"+name+"'"); if(data.equals("true")==true){ com.kingdee.eas.util.app.DbUtil.execute(ctx, sql.toString()); }else{ result = "删除失败"; } //} return result; } @Override protected String _preget(Context ctx, String name) throws BOSException { // TODO Auto-generated method stub Map<String, String> mapParam = new HashMap<String, String>(); JSONObject rejson = new JSONObject(); StringBuffer sql = new StringBuffer(); StringBuffer sqls = new StringBuffer(); sql.append("select "); sql.append("fid,fname_l2,fesid,FHROrgUnitID "); sql.append("from "); sql.append("CT_TES_pxbperson t "); sql.append("where "); sql.append("fname_l2 = '"+name+"'"); // 数据库操作 String ss = ""; String s = ""; IRowSet rs = com.kingdee.eas.util.app.DbUtil.executeQuery(ctx, sql.toString()); try { if (rs.size()==0) { ss="员工不存在"; } else { while (rs.next()){ rejson.put("id", rs.getString("fid")); rejson.put("name", rs.getString("fname_l2")); rejson.put("orgid", rs.getString("FHROrgUnitID")); rejson.put("esid", rs.getString("FEsid")); s=rs.getString("fid"); ss = rejson.toString(); } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } mapParam.put("id", s); String pathUrl = "http://www.91pxb.com/api/Companies/Get"; // String result = PostHttp(pathUrl, mapParam); // 测试数据 String result = "{/"err/": 0,/"data/": {/"id/": 12580,/"parent_id/": 91666,/"organization_name/":/"开发部/",/"organization_url/": /"上海淘客网/开发部/",}}"; JSONObject json = JSONObject.fromObject(result); JSONObject jsondata = json.getJSONObject("data"); String fparent_id = jsondata.getString("parent_id"); String id = jsondata.getString("id"); String organization_name = jsondata.getString("organization_name"); String organization_url = jsondata.getString("organization_url"); System.out.println(ss); return ss; } @Override protected String _preup(Context ctx, String id, String name, String orgid,String esid) throws BOSException { // TODO Auto-generated method stub Map<String, String> mapParam = new HashMap<String, String>(); int pxbid = (int) ((Math.random() * 9 + 1) * 10000); //String pxbid = Integer.toString(id); String pxbids =Integer.toString(pxbid); mapParam.put("id",pxbids); String pathUrl = "http://www.91pxb.com/api/Employees/Delete"; //培训宝API //String result = PostHttp(pathUrl, mapParam); //if(判断api返回结果){ JSONObject rejson = new JSONObject(); Date date = new Date(); StringBuffer sql = new StringBuffer(); StringBuffer sqls = new StringBuffer(); String ss = ""; String s = ""; sql.append("select "); sql.append("fid,fname_l2,FHROrgUnitID,fesid "); sql.append("from "); sql.append("CT_TES_pxbperson t "); sql.append("where "); sql.append("FEsid = '"+id+"'"); sqls.append("UPDATE "); sqls.append("CT_TES_pxbperson "); sqls.append("SET "); sqls.append("FHROrgUnitID = '"+orgid+"'"); sqls.append(" where FEsid = '"+id+"' "); // 数据库操作 //System.out.println(sqls.toString()); IRowSet rs = com.kingdee.eas.util.app.DbUtil.executeQuery(ctx, sql.toString()); if (rs.size()==0) { ss="组织不存在"; } else { try { while (rs.next()) { //pxbid=rs.getString("fid"); com.kingdee.eas.util.app.DbUtil.execute(ctx, sqls.toString()); ss = "更新完成"; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //} return ss; } public static String PostHttp(String url, Map<String, String> paramMap){ PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // conn.setRequestProperty("Charset", "UTF-8"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 设置请求属性 String param = ""; if (paramMap != null && paramMap.size() > 0) { Iterator<String> ite = paramMap.keySet().iterator(); while (ite.hasNext()) { String key = ite.next();// key String value = paramMap.get(key); param += key + "=" + value + "&"; } param = param.substring(0, param.length() - 1); } // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.err.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }}

扩展链接

JAVA POST api方法可参考



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表