K/3 Cloud Web API集成开发Java完整示例【分享】
金蝶云社区-云社区用户4u731234
云社区用户4u731234
9人赞赏了该文章 4.3万次浏览 未经作者许可,禁止转载编辑于2015年09月15日 14:38:18

先上图:
Web API 【销售订单】保存完整示例分享。


Save 新增修改
View 查看
Submit 提交
Audit 审核
UnAudit 反审核
StatusConvert 状态转换

核心调用代码类:
[code]import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;

public class InvokeHelper {

// K3 Cloud WebSite URL Example "http://192.168.19.113/K3Cloud/"
public static String POST_K3CloudURL = "http://192.168.19.113/K3Cloud/";

// Cookie 值
private static String CookieVal = null;

private static Map map = new HashMap();
static {
map.put("Save",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");
map.put("View",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc");
map.put("Submit",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");
map.put("Audit",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");
map.put("UnAudit",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc");
map.put("StatusConvert",
"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc");
}

// HttpURLConnection
private static HttpURLConnection initUrlConn(String url, JSONArray paras)
throws Exception {
URL postUrl = new URL(POST_K3CloudURL.concat(url));
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
if (CookieVal != null) {
connection.setRequestProperty("Cookie", CookieVal);
}
if (!connection.getDoOutput()) {
connection.setDoOutput(true);
}
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());

UUID uuid = UUID.randomUUID();
int hashCode = uuid.toString().hashCode();

JSONObject jObj = new JSONObject();

jObj.put("format", 1);
jObj.put("useragent", "ApiClient");
jObj.put("rid", hashCode);
jObj.put("parameters", chinaToUnicode(paras.toString()));
jObj.put("timestamp", new Date().toString());
jObj.put("v", "1.0");

out.writeBytes(jObj.toString());
out.flush();
out.close();

return connection;
}

// Login
public static boolean Login(String dbId, String user, String pwd, int lang)
throws Exception {

boolean bResult = false;

String sUrl = "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc";

JSONArray jParas = new JSONArray();
jParas.put(dbId);// 帐套Id
jParas.put(user);// 用户名
jParas.put(pwd);// 密码
jParas.put(lang);// 语言

HttpURLConnection connection = initUrlConn(sUrl, jParas);
// 获取Cookie
String key = null;
for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) {
if (key.equalsIgnoreCase("Set-Cookie")) {
String tempCookieVal = connection.getHeaderField(i);
if (tempCookieVal.startsWith("kdservice-sessionid")) {
CookieVal = tempCookieVal;
break;
}
}
}

BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= ");
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
bResult = line.contains("\"LoginResultType\":1");
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();

connection.disconnect();

return bResult;
}

// Save
public static void Save(String formId, String content) throws Exception {
Invoke("Save", formId, content);
}

// View
public static void View(String formId, String content) throws Exception {
Invoke("View", formId, content);
}

// Submit
public static void Submit(String formId, String content) throws Exception {
Invoke("Submit", formId, content);
}

// Audit
public static void Audit(String formId, String content) throws Exception {
Invoke("Audit", formId, content);
}

// UnAudit
public static void UnAudit(String formId, String content) throws Exception {
Invoke("UnAudit", formId, content);
}

// StatusConvert
public static void StatusConvert(String formId, String content)
throws Exception {
Invoke("StatusConvert", formId, content);
}

private static void Invoke(String deal, String formId, String content)
throws Exception {

String sUrl = map.get(deal).toString();
JSONArray jParas = new JSONArray();
jParas.put(formId);
jParas.put(content);

HttpURLConnection connectionInvoke = initUrlConn(sUrl, jParas);

BufferedReader reader = new BufferedReader(new InputStreamReader(
connectionInvoke.getInputStream()));

String line;
System.out.println(" ============================= ");
System.out.println(" Contents of post request ");
System.out.println(" ============================= ");
while ((line = reader.readLine()) != null) {
String sResult = new String(line.getBytes(), "utf-8");
System.out.println(sResult);
}
System.out.println(" ============================= ");
System.out.println(" Contents of post request ends ");
System.out.println(" ============================= ");
reader.close();

connectionInvoke.disconnect();
}

/**
* 把中文转成Unicode码
*
* @param str
* @return
*/
public static String chinaToUnicode(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
int chr1 = (char) str.charAt(i);
if (chr1 >= 19968 && chr1 <= 171941) {// 汉字范围 \u4e00-\u9fa5 (中文)
result += "\\u" + Integer.toHexString(chr1);
} else {
result += str.charAt(i);
}
}
return result;
}
}
[/code]

Main测试类:
[code]public class InvokeTest {

public static void main(String[] args) throws Exception {
InvokeHelper.POST_K3CloudURL = "http://192.168.19.113/K3Cloud/";
String dbId = "55bb19192ebcde";
String uid = "Demo";
String pwd = "888888";
int lang = 2052;

if (InvokeHelper.Login(dbId, uid, pwd, lang)) {

// 销售订单保存测试
// 业务对象Id
String sFormId = "SAL_SaleOrder";
//需要保存的数据
// 如下字段可能需要根据自己实际值做修改
// FCustId FSalerId FMaterialId FUnitID
String sContent = "{\"Creator\":\"String\",\"NeedUpDateFields\":[\"FBillTypeID\",\"FDate\",\"FBusinessType\",\"FSaleOrgId\",\"FCustId\",\"FSettleCurrId\",\"FSalerId\",\"SAL_SaleOrder__FSaleOrderEntry\",\"FMaterialId\",\"FSettleOrgIds\",\"FUnitID\",\"FQty\",\"SAL_SaleOrder__FSaleOrderFinance\",\"FSettleCurrId\",\"FLocalCurrId\",\"FIsIncludedTax\",\"FBillTaxAmount\",\"FBillAmount\",\"FBillAllAmount\",\"FExchangeTypeId\",\"FExchangeRate\"],\"Model\":{\"FID\":0,\"FBillTypeID\":{\"FNumber\":\"XSDD01_SYS\"},\"FBusinessType\":\"NORMAL\",\"FSaleOrgId\":{\"FNUMBER\":\"100\"},\"FCustId\":{\"FNUMBER\":\"CUST0001\"},\"FSettleCurrId\":{\"FNUMBER\":\"PRE001\"},\"FSalerId\":{\"FNUMBER\":\"0002\"},\"SAL_SaleOrder__FSaleOrderFinance\":{\"FExchangeRate\":6.8123},\"SAL_SaleOrder__FSaleOrderEntry\":[{\"FMaterialId\":{\"FNUMBER\":\"001\"},\"FSettleOrgIds\":{\"FNUMBER\":\"100\"},\"FUnitID\":{\"FNumber\":\"个\"},\"FQty\":10}]}}";
InvokeHelper.Save(sFormId, sContent);

System.out.println("hola success");
}
}
}
[/code]
2个.java类 可这里下载