Chinaunix

标题: 用gsoap返回结构体数组遇到的问题?只返回了一个结构? [打印本页]

作者: explorewen    时间: 2014-04-29 09:37
标题: 用gsoap返回结构体数组遇到的问题?只返回了一个结构?
接口要求实现输入一个查询条件,返回一组结构,类似于查询明细。这样想来是要返回结构体数组的。
现在的问题是我在客户端测试时只能得到数组个数 和 第一个结构的内容,是哪里出错了呢?是服务端的实现有问题 还是 客户端取值有问题啊?

这个问题已经困扰我几天了,有哪位知道给指点下啊,代码如下:

/**服务端接口定义如下**/
struct ns__BankCard
{
    xsd__string cardholderName;
    xsd__string nationalId;
};

struct ns__GetAccountBankCardListResponse
{
    struct ns__BankCard **cardList;
    xsd__int __size;
};

int ns__nsGetAccountBankCardList(xsd__string accountId,struct ns__GetAccountBankCardListResponse *response);

/**我是这样实现接口函数的,有问题吗**/
int ns__nsGetAccountBankCardList(struct soap *soap,xsd__string accountId,struct ns__GetAccountBankCardListResponse *response)
{
    response->__size=2;
    response->cardList= malloc((response->__size+1)*sizeof(struct ns__BankCard *));
    //response->cardList= malloc((response->__size+1)*sizeof(*response->cardList));

    response->cardList[0]=(struct ns__BankCard *)malloc(sizeof(struct ns__BankCard ));
    (*response->cardList[0]).cardholderName = (char *)malloc(32);
    strcpy((*response->cardList[0]).cardholderName,"zhangsan");

    response->cardList[1]=(struct ns__BankCard *)malloc(sizeof(struct ns__BankCard ));
    (*response->cardList[1]).cardholderName = (char *)malloc(32);
    strcpy((*response->cardList[1]).cardholderName,"lisi");
        return SOAP_OK;
}


/*下面是客户端调用主要代码*/
        memset(accountId_In,0x00,sizeof(accountId_In));
        strcpy(accountId_In,argv[2]);

        struct ns2__nsGetAccountBankCardListResponse r3;
        soap_call_ns2__nsGetAccountBankCardList(&soap,server,"", accountId_In, &r3);
        printf("size=%d\n",r3._x002dsize);/*此行可得到正确的值2*/
        int i;
        for(i=0;i<r3._x002dsize;i++)/*只能打印出第一个结构,第二个结构是乱的,哪里有错了?*/
        {
            printf("[%d]\n",(r3.cardList+i)->cardType);
            printf("[%s]\n",(r3.cardList+i)->cardholderName);
            printf("\n");
        }
作者: explorewen    时间: 2014-04-29 15:38
还没有人回答啊,大神们关注下啊
作者: folklore    时间: 2014-04-29 15:46
没用过Soap, 顶顶更健康~~

作者: asker123    时间: 2014-04-29 16:25
回复 1# explorewen

好好看看结构体的定义是不是跟你想得一样
作者: wujiajia    时间: 2014-04-29 16:46
struct ns__BankCard
{
    xsd__string cardholderName;
    xsd__string nationalId;
};

struct ns__GetAccountBankCardListResponse
{
    struct ns__BankCard **cardList;
    xsd__int __size;
};

int ns__nsGetAccountBankCardList(xsd__string accountId,struct ns__GetAccountBankCardListResponse *response);


int ns__nsGetAccountBankCardList(struct soap *soap,xsd__string accountId,struct ns__GetAccountBankCardListResponse *response)
{
    response->__size=2;
    response->cardList= (struct ns__BankCard **)soap_malloc( sizeof(struct ns__BankCard *));
    for (int i=0i i<response->__size;i++) {

    response->cardList[i]=(struct ns__BankCard *)malloc(sizeof(struct ns__BankCard ));
    response->cardList[i]->cardholderName = soap_strdup(soap,"zhangsan");
   }

        return SOAP_OK;
}
作者: sjtu_smile    时间: 2014-04-29 16:46
回复 1# explorewen

   response->cardList= malloc((response->__size+1)*sizeof(struct ns__BankCard *));

这一句错了, 改成:

   response->cardList= malloc(sizeof(struct ns__BankCard *));


   
作者: sjtu_smile    时间: 2014-04-29 16:54
回复 6# sjtu_smile

看错了,没问题


   
作者: explorewen    时间: 2014-04-30 08:45
回复 5# wujiajia

你的修改我用过了,结果还和以前一样。
另外
response->cardList=(struct ns__BankCard **)soap_malloc(soap,(response->__size)*sizeof(struct ns__BankCard *));
   
作者: wujiajia    时间: 2014-04-30 14:54
本帖最后由 wujiajia 于 2014-04-30 14:59 编辑

/**服务端接口定义如下**/
struct ns__BankCard
{
    xsd__string cardholderName;
    xsd__string nationalId;
};

struct ns__GetAccountBankCardListResponse
{
    $int __size;
    struct ns__BankCard *cardList;
   
};
int ns__nsGetAccountBankCardList(struct soap *soap,xsd__string accountId,struct ns__GetAccountBankCardListResponse *response)
{
    response->__size=2;
    response->cardList=(struct ns__BankCard *)soap_malloc(response->__size*sizeof(struct ns__BankCard ));
    for (int i=0i i<response->__size;i++) {
        response->cardList->cardholderName = soap_strdup(soap,"zhangsan");
   }

        return SOAP_OK;
}//试试这样
作者: explorewen    时间: 2014-04-30 17:32
回复 9# wujiajia
这样也不行,试过。
接口定义成 $int __size;时,客户端生成.h后得不到__size属性了,只剩下cardList了。
struct ns2__GetAccountBankCardListResponse
{
    struct ns2__BankCard*               cardList;
};

   
作者: explorewen    时间: 2014-05-12 17:01
此问题已解决,回来分享下。
/**********测试开始**********/
struct ns__card
{
xsd__string holdername;
xsd__string value;
};

struct ns__cardList
{
struct ns__card *__ptr;
int __size;
};

int ns__testfun(xsd__string bufin,struct ns__cardList *list);

/*服务端程序*/
int ns__testfun(struct soap *soap,xsd__string bufin,struct ns__cardList
*list)
{
printf("in=[%s]\n",bufin);
list->__size =2;


list->__ptr = soap_malloc(soap,list->__size*sizeof(struct ns__card));

int i=0;
for(i=0;i<list->__size;i++)
{
if (i==0)
{
(list->__ptr+i)->holdername = soap_strdup(soap,"zhangsan");
(list->__ptr+i)->value= soap_strdup(soap,"123");
}
else
{
(list->__ptr+i)->holdername = soap_strdup(soap,"lisi");
(list->__ptr+i)->value= soap_strdup(soap,"789");
}
}

for(i=0;i<list->__size;i++)
{
printf("name=[%s]\n",(list->__ptr+i)->holdername);
printf("\n");
}
return SOAP_OK;
}

/*客户端程序*/
printf("hello\n");
struct ns2__testfunResponse rest;
soap_call_ns2__testfun( &soap, server, "","abc" , &rest);
printf("size=[%d]\n",rest.list->__sizeitem);
int t;
for(t=0;t<rest.list->__sizeitem;t++)
{
//printf("name=[%s]\n",rest.list->item->holdername);
printf("name=[%s]\n",(rest.list->item+t)->holdername);
printf("name=[%s]\n",(rest.list->item+t)->value);
}
printf("ok\n"




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2