记录一下开发过程遇到的坑。因为金蝶云在webapi开发能查阅到的资料少之又少,java有些SDK包用不了,或者版本不兼容等各种问题。故再此记录一下用okhttp的方法来调用api接口。
接上一篇博客,获取到cookie后,传到数据页(emmm。。。activity传值,和fragment传值都会吧?我布局里用到fragment,自己挖坑给自己跳。。。后面总结篇会讲这些坑。。。)
好了,言归正传,继续码代码。
打开金蝶云星空客户端,查看webapi开发。看一下接口参数。
依旧是看这个接口参数。
必录的字段必须传值,非必录就可以不用写。我觉得最关键的还是JSON的构造。
我整理好了请求的JSON,如下:
{
"data":{
"FormId":"STK_InStock",
"TopRowCount":0,
"FilterString":"fcreatorid='792671'",
"Limit":100,
"StartRow":0,
"FieldKeys":"fBillNo,FInStockEntry_Link_FSBillId,fdate",
"OrderString":"fBillNo desc"
}
}
"FormId":"STK_InStock", //这个是表单id,必传的
"TopRowCount":0,
"FilterString":"fcreatorid='792671'", //这个是过滤字段,若是多个,就把FilterString构造成JSONarry,放入要过滤的字段的键值对。
"Limit":100,// 这是查询返回结果的行数,0则为不限制(所有结果都返回)
"StartRow":0,//从第几行开始
"FieldKeys":"fBillNo,FInStockEntry_Link_FSBillId,fdate",//这里是要查询的字段
"OrderString":"fBillNo desc"//按照什么什么字段排序
postman测试返回结果没问题,直接就开线程构造请求函数。cookie放入请求头就好了。
因为返回结果是数组,我布局用listview显示出来,我把每个数组的值都取出来,定义一个对象,存放返回结果的string[],通过adapter填充进去。在线程调用 runOnUiThread(new Runnable()更新UI界面。
public void filt(String billd){
new Thread(new Runnable() {
String url ="http://公司域名/k3cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc";
@Override
public void run() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("FormId", "PUR_ReceiveBill");//PUR_ReceiveBill
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonObject.put("TopRowCount", 0); //HTTP HEADER
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonObject.put("FilterString","fBillNo='"+billd+"'");
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonObject.put("Limit", 100);
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonObject.put("StartRow", 0);
} catch (JSONException e) {
e.printStackTrace();
}
try {
jsonObject.put("FieldKeys", "fBillNo,FDate,FSupplierid,FPurchaserId,FMaterialId.FNumber," +//4
"FMaterialName,FMateriaModel,FBaseUnitId," +//7
"FActReceiveQty,FDescription ,FStockID,FPriceUnitQty,FPriceUnitId,FUnitId,FID,FDetailEntity_FEntryID,FCloseStatus,F_abcd_Decimal");
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonObject1 = new JSONObject();
try {
jsonObject1.put("data", jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody myrequsetbody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonObject1.toString());
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.post(myrequsetbody)
.url(url)
.addHeader("Content-Type", "application/json")
.addHeader("User-Agent", "PostmanRuntime/7.29.2")
.addHeader("Accept-Encoding", "gzip, deflate, br")
.addHeader("Connection", "keep-alive")
.addHeader("Accept", "*/*")
.addHeader("Cookie",token)
.build();
Log.d("qy1314", "" + jsonObject);
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
String date = response.body().string();
if(date.length()!=189) {
Log.d("qy1314", "res_json" + response);
json = new JSONArray(date);
JSONArray[] array1 =new JSONArray[json.length()];
for(int i=0;i<json.length();i++) {
array1[i]=json.getJSONArray(i);
}
String[] ar=new String[array1.length];
for(int i=0;i<array1.length-1;i++)
{
Log.e("qy1314", "bill[i]"+array1[i] );
}
statue=new String[json.length()];
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < json.length(); i++) {
try {
statue[i] = json.getJSONArray(i).getString(16);
} catch (JSONException e) {
e.printStackTrace();
}
}
if(statue[0].isEmpty()|statue==null)
{
Toast.makeText(MainActivity_zhengdantaodaruku.this,"请输入正确的单据编号",Toast.LENGTH_LONG).show();
}else
{
if (statue[0].equals("B")) {
Toast.makeText(MainActivity_zhengdantaodaruku.this, "该单据已关闭或者已入库!", Toast.LENGTH_LONG).show();
}
else {
array=new String[json.length()];
djbh=new String[json.length()];
fdate=new String[json.length()];
suport=new String[json.length()];
oerder=new String[json.length()];
modul=new String[json.length()];
nuber=new String[json.length()];
name=new String[json.length()];
base=new String[json.length()];
shsl=new String[json.length()];
hgs=new String[json.length()];
note=new String[json.length()];
index=new int[json.length()];
stork=new String[json.length()];
entry=new String[json.length()];
FID=new String[json.length()];
wjhsl=new String[json.length()];
for (int i = 0; i < json.length(); i++) {
try {
djbh[i] = json.getJSONArray(i).getString(0);
fdate[i] = json.getJSONArray(i).getString(1);
suport[i] = json.getJSONArray(i).getString(2);
oerder[i] = json.getJSONArray(i).getString(3);
nuber[i] = json.getJSONArray(i).getString(4);
name[i] = json.getJSONArray(i).getString(5);
modul[i] = json.getJSONArray(i).getString(6);
base[i] = json.getJSONArray(i).getString(12);
shsl[i] = json.getJSONArray(i).getString(8);
hgs[i] = json.getJSONArray(i).getString(8);
note[i] = json.getJSONArray(i).getString(9);
stork[i] = json.getJSONArray(i).getString(10);
entry[i] = json.getJSONArray(i).getString(15);
FID[i]=json.getJSONArray(i).getString(14);
wjhsl[i]=json.getJSONArray(i).getString(17);
index[i] = i;
String uit = base[i];
// filt2(uit);
// DW[i]=dj.getText().toString();
array[i] = "序 号:" + (index[i] + 1) + "\n" +
"物料名称:" + name[i] + "\n" +
"规格型号:" + modul[i] + "\n" +
"送货数量:" + shsl[i] + "\n" +
"合格数量:" + hgs[i]+"\n"+
"未交货数量"+wjhsl[i];
// Thread.sleep(200);
Log.d("ckid", stork[i]);
} catch (JSONException e) {
e.printStackTrace();
}
}
// setdata(array);
setdata(array, fdate, djbh, suport, oerder, nuber, name, modul, base, shsl, hgs, note, index, stork,wjhsl);
text.setText(djbh[0]);
setJsonObject(json);
adapter aaa = new adapter(MainActivity_zhengdantaodaruku.this, R.layout.layout, carList);
listView.setAdapter(aaa);
}
}
}
});
}
else
{
Message msg =Message.obtain();
msg.what=2;
myHandler.handleMessage(msg);
}
} catch (IOException e) {
e.printStackTrace();
//response
} catch (JSONException e) {
e.printStackTrace();
//jsonobject
}
}
}).start();
}