基于gsoap开发WebService服务返回结构体数组
(1)返回的目标结构,开头以ns__,这与第一行//gsoap ns XXX 相关,例如
struct ns__EmployeeInfo { int userid; char* name; };
struct ArrayOfEmp2 {struct ns__EmployeeInfo info;};
接口函数:int ns__getEmp2(void *_,struct ArrayOfEmp2 &ccc);
下面是“处女座”的写法:
好处就是,在客户端返回值直接就是EmployeeInfo结构。
struct ns__ArrayOfEmp2 {struct ns__EmployeeInfo info;}; 接口函数:int ns__getEmp2(void *_,struct ns__ArrayOfEmp2 &ccc);
(3)重点来了,返回结构体数组的写法:
struct ArrayOfEmp1 //此为正确写法 {struct ns__EmployeeInfo **__ptr;int __size;};
接口函数:int ns__getAllEmp1(void *_,struct ArrayOfEmp1 &alal);几个注意点:父级结构名字不要带ns__;返回的目标结构成员的名字必须是__ptr,大小的名字必须是__size。
(4)接口实现代码
int ns__getAllEmp1(struct soaP* soap,void *_,struct ArrayOfEmp1 &_return) { _return.__size = 5; _return.__ptr = (struct ns__EmployeeInfo**)soap_malloc(soap,5*sizeof(struct ns__EmployeeInfo)); _return.__ptr[0] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo)); _return.__ptr[0]->userid=1000; _return.__ptr[0]->name= soap_strdup(soap,"name1"); _return.__ptr[1] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo)); _return.__ptr[1]->userid=1001; _return.__ptr[1]->name= soap_strdup(soap,"name2"); _return.__ptr[2] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo)); _return.__ptr[2]->userid=1002; //_return.__ptr[2]->name = soap_strdup(soap,"name3"); _return.__ptr[2]->name = (char*)"name3";//这种形式的复制,也是没关系的 _return.__ptr[3] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo)); _return.__ptr[3]->userid = 1003; _return.__ptr[3]->name = (char*)"name4"; _return.__ptr[4] = (struct ns__EmployeeInfo*)soap_malloc(soap,sizeof(struct ns__EmployeeInfo)); _return.__ptr[4]->userid = t.id; _return.__ptr[4]->name = soap_strdup(soap,t.name.c_str()); return SOAP_OK; }
几个作死行为:
1、返回结构数组的父级结构体,写法上带ns__,如:
struct ns__ArrayOfEmp //此写法会导致返回的结构是ArrayOfEmp,且其Item属性为NULL {struct ns__EmployeeInfo **__ptr;int __size;};
====以下无内容====
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。