JAVA对接金蝶云星空7.3

一、登录、查询代码示例

  1 /**
  2      * 处理数据,在各自档案service实现逻辑
  3      * @param
  4      * @return
  5      */
  6     public abstract Map<String, Object> syncData (String result);
  7 
  8 
  9 
 10     /**
 11      * 返回请求参数
 12      * @return
 13      */
 14     public abstract JsonArray getRequestData();
 15 
 16     /**
 17      * 调用查询接口
 18      * @return
 19      */
 20     public String getQueryAdress(){
 21         return baseProperties.getBillQueryUrl();
 22     }
 23 
 24 
 25     public boolean login() throws Exception {
 26 
 27         boolean bResult = false;
 28         JsonArray jParas = new JsonArray();
 29         jParas.add(baseProperties.getDbId());
 30         jParas.add(baseProperties.getUserName());
 31         jParas.add(baseProperties.getPwd());
 32         jParas.add(baseProperties.getLang());
 33         HttpURLConnection connection = initUrlConn(baseProperties.getLoginUrl(), jParas);
 34         String key = null;
 35         for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
 36             if (key.equalsIgnoreCase("Set-Cookie")) {
 37                 String tempCookieVal = connection.getHeaderField(i);
 38                 if (tempCookieVal.startsWith("kdservice-sessionid")) {
 39                     CookieVal = tempCookieVal;
 40                     break;
 41                 }
 42             }
 43         }
 44 
 45         BufferedReader reader = new BufferedReader(new InputStreamReader(
 46                 connection.getInputStream()));
 47         String line;
 48         while ((line = reader.readLine()) != null) {
 49             String sResult = new String(line.getBytes(), "utf-8");
 50             bResult = line.contains("\"LoginResultType\":1");
 51         }
 52         reader.close();
 53         connection.disconnect();
 54         return bResult;
 55     }
 56 
 57     private  HttpURLConnection initUrlConn(String url, JsonArray paras)
 58             throws Exception {
 59         URL postUrl = new URL(baseProperties.getBaseUrl().concat(url));
 60         HttpURLConnection connection = (HttpURLConnection) postUrl
 61                 .openConnection();
 62         if (CookieVal != null) {
 63             connection.setRequestProperty("Cookie", CookieVal);
 64         }
 65         if (!connection.getDoOutput()) {
 66             connection.setDoOutput(true);
 67         }
 68         connection.setRequestMethod("POST");
 69         connection.setUseCaches(false);
 70         connection.setInstanceFollowRedirects(true);
 71         connection.setRequestProperty("Content-Type", "application/json");
 72         DataOutputStream out = new DataOutputStream(
 73                 connection.getOutputStream());
 74 
 75         UUID uuid = UUID.randomUUID();
 76         int hashCode = uuid.toString().hashCode();
 77 
 78         JsonObject jObj = new JsonObject();
 79         jObj.addProperty("format", 1);
 80         jObj.addProperty("useragent", "ApiClient");
 81         jObj.addProperty("rid", hashCode);
 82         jObj.addProperty("parameters", chinaToUnicode(paras.toString()));
 83         jObj.addProperty("timestamp", new Date().toString());
 84         jObj.addProperty("v", "1.0");
 85 
 86         out.writeBytes(jObj.toString());
 87         out.flush();
 88         out.close();
 89 
 90         return connection;
 91     }
 92 
 93 
 94     /**
 95      * 拉取数据并处理
 96      * @return
 97      */
 98     public Map<String, Object> datax() throws Exception {
 99         login();
100         HttpURLConnection connectionInvoke = initUrlConn(getQueryAdress(), getRequestData());
101         BufferedReader reader = new BufferedReader(new InputStreamReader(
102                 connectionInvoke.getInputStream()));
103         String line;
104         String sResult = null;
105         while ((line = reader.readLine()) != null) {
106             sResult = new String(line.getBytes(), "utf-8");
107             System.out.println(sResult);
108         }
109         if(sResult.contains("\"IsSuccess\":false")){
110             ErrorEntity errorEntity = JSONUtil.toBean(JSONUtil.parseFromMap(objectMapper.readValue(sResult,Map.class)),ErrorEntity.class);
111             throw new InfException(StrUtil.format("数据同步失败,当前同步类型为:{},返回信息为:{}",getCurrentType().getTitle(), errorEntity.getResult().getResponseStatus().getErrors().get(0).getMessage()));
112         }
113         reader.close();
114         connectionInvoke.disconnect();
115         Map<String, Object> syncMap = syncData(sResult);
116         return syncMap;
117     }
118 
119     public static String chinaToUnicode(String str) {
120         String result = "";
121         for (int i = 0; i < str.length(); i++) {
122             int chr1 = (char) str.charAt(i);
123             if (chr1 >= 19968 && chr1 <= 171941) {
124                 result += "\\u" + Integer.toHexString(chr1);
125             } else {
126                 result += str.charAt(i);
127             }
128         }
129         return result;
130     }

二、入库单保存接口参数示例

1         JsonArray jParas = new JsonArray();
2         String formid = "STK_InStock";
3         jParas.add(formid);
4         jParas.add(data.toString());    

猜你喜欢

转载自www.cnblogs.com/lg193/p/13365533.html