xmlrpc-c适合初学者,示例代码不起作用 - >预期的XMLRPC_TYPE_ARRAY

时间:2011-06-17 16:45:42

标签: c xml-rpc

我打算通过以下基本代码在控制台上显示xml格式:

#include <stdio.h>
#include <stdlib.h>
#include <xmlrpc.h>
#include <xmlrpc_client.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>

#define XMLRPC_NAME       "XML-RPC tesaja"
#define XMLRPC_VERSION    "0.1"

int main()
{
//declare
    xmlrpc_env env;
    xmlrpc_value *result = NULL;

    xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, XMLRPC_NAME, XMLRPC_VERSION);
    xmlrpc_env_init(&env);


     result = xmlrpc_client_call(&env, "http://xmlrpc-c.sourceforge.net/api/sample.php" ,
                                 "sample.sumAndDifference", "(ii)", //method
                                (xmlrpc_int32) 5,                  //var
                                (xmlrpc_int32) 3);                 //var

    /* ma code */
     char output[1024] = {0};
     int l_output = 0;
    /* ------------------ keluarin xml ------------------*/
        xmlrpc_value *params;
        xmlrpc_mem_block *xmlmemblockP = NULL;

    params = xmlrpc_build_value(&env, "(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 7);    
    xmlmemblockP = XMLRPC_MEMBLOCK_NEW(char, &env, 0);
    xmlrpc_serialize_call(&env, xmlmemblockP, "sample.sumAndDifference", params);


    l_output = snprintf(output,XMLRPC_MEMBLOCK_SIZE(char, xmlmemblockP),"%s",XMLRPC_MEMBLOCK_CONTENTS(char, xmlmemblockP));
    output[l_output] = '\0';

    printf("submit data -> \n %s\n",output);

    XMLRPC_MEMBLOCK_FREE(char, xmlmemblockP);

    /* Dispose of our parameter array. */
    xmlrpc_DECREF(params);

     /* ------------------ keluarin xml ------------------*/


    if(env.fault_occurred)
    {
    printf("%s\n",env.fault_string);
        return 0;
    }


    // Parse our result value 
    xmlrpc_int32 sum, difference;
    xmlrpc_decompose_value(&env, result, "{s:i,s:i,*}",
                       "sum", &sum,
                       "difference", &difference);

    /* keluarin xml respon */
    params = result;
    xmlmemblockP = XMLRPC_MEMBLOCK_NEW(char, &env, 0);
    xmlrpc_serialize_call(&env, xmlmemblockP, "sample.sumAndDifference", params);

    l_output = snprintf(output,XMLRPC_MEMBLOCK_SIZE(char, xmlmemblockP),"%s",XMLRPC_MEMBLOCK_CONTENTS(char, xmlmemblockP));
    output[l_output] = '\0';    
    printf("respon data-> \n %s \n",output);
    XMLRPC_MEMBLOCK_FREE(char, xmlmemblockP);

    /* Dispose of our parameter array. */
    //xmlrpc_DECREF(params); //udah diwakilin dibawah
    /* keluarin xml respon*/


    if(env.fault_occurred)
    {
        printf("%s\n",env.fault_string);
        return 0;
    }


    // Print out our sum and difference.  
    printf("Sum: %d, Difference: %d\n", (int) sum, (int) difference);


    // Dispose of our result value.  
    xmlrpc_DECREF(result);

    //Shutdown our XML-RPC client library. 
    xmlrpc_env_clean(&env);
    xmlrpc_client_cleanup();



return 0;
}

以某种方式它无法正常工作:

编译

$ gcc -o xxx xxx.c `xmlrpc-c-config libwww-client --libs --cflags`

输出

submit data -> 
 <?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.sumAndDifference</methodName>
<params>
<param><value><i4>5</i4></value></param>
<param><value><i4>7</i4></value></param>
</params>
</methodCall>
respon data-> 
 <?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.sumAndDifference</methodName>
 params>
Expected XMLRPC_TYPE_ARRAY

问题

  1. 为什么这些代码无法正常工作?
  2. 什么是<i4>是什么意思?,如果xmlrpc服务器(任何基于xmlrpc,如xmlrpc-c,xmlrpc java等等)请求integer<i4>代表它吗?
  3. 所有事情

2 个答案:

答案 0 :(得分:0)

啊,在我寻找xmlrpc-c库提供的功能后,我找到xmlrpc_serialize_response();,我认为会做这样的事情

#include <stdio.h>
#include <stdlib.h>
#include <xmlrpc.h>
#include <xmlrpc_client.h>
#include <xmlrpc-c/base.h>
#include <xmlrpc-c/util.h>

#define XMLRPC_NAME       "XML-RPC tesaja"
#define XMLRPC_VERSION    "0.1"

int main()
{
//declare
    xmlrpc_env env;
    xmlrpc_value *result = NULL;

    xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, XMLRPC_NAME, XMLRPC_VERSION);
    xmlrpc_env_init(&env);


     result = xmlrpc_client_call(&env, "http://xmlrpc-c.sourceforge.net/api/sample.php" ,
                                 "sample.sumAndDifference", "(ii)", //method
                                (xmlrpc_int32) 5,                  //var
                                (xmlrpc_int32) 3);                 //var

    /* ma code */
     char output[1024] = {0};
     int l_output = 0;
    /* ------------------ keluarin xml ------------------*/
        xmlrpc_value *params;
        xmlrpc_mem_block *xmlmemblockP = NULL;

    params = xmlrpc_build_value(&env, "(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 7);    
    xmlmemblockP = XMLRPC_MEMBLOCK_NEW(char, &env, 0);
    xmlrpc_serialize_call(&env, xmlmemblockP, "sample.sumAndDifference", params);


    l_output = snprintf(output,XMLRPC_MEMBLOCK_SIZE(char, xmlmemblockP),"%s",XMLRPC_MEMBLOCK_CONTENTS(char, xmlmemblockP));
    output[l_output] = '\0';

    printf("submit data -> \n %s\n",output);

    XMLRPC_MEMBLOCK_FREE(char, xmlmemblockP);

    /* Dispose of our parameter array. */
    xmlrpc_DECREF(params);

     /* ------------------ keluarin xml ------------------*/


    if(env.fault_occurred)
    {
    printf("err : %s\n",env.fault_string);
        return 0;
    }


    // Parse our result value 
    xmlrpc_int32 sum, difference;

    xmlrpc_decompose_value(&env, result, "{s:i,s:i,*}",
                       "sum", &sum,
                       "difference", &difference);

    /* keluarin xml respon */

    xmlmemblockP = XMLRPC_MEMBLOCK_NEW(char, &env, 0);

    xmlrpc_serialize_response(&env,xmlmemblockP,result);    
    l_output = snprintf(output,XMLRPC_MEMBLOCK_SIZE(char, xmlmemblockP),"%s",XMLRPC_MEMBLOCK_CONTENTS(char, xmlmemblockP));
    output[l_output] = '\0';    
    printf("respon data-> \n %s \n",output);
    XMLRPC_MEMBLOCK_FREE(char, xmlmemblockP);

    /* keluarin xml respon*/


    if(env.fault_occurred)
    {
        printf("err : %s\n",env.fault_string);
        return 0;
    }


    // Print out our sum and difference.  
    printf("Sum: %d, Difference: %d\n", (int) sum, (int) difference);


    // Dispose of our result value.  
    xmlrpc_DECREF(result);

    //Shutdown our XML-RPC client library. 
    xmlrpc_env_clean(&env);
    xmlrpc_client_cleanup();



return 0;
}

<强>输出

submit data -> 
 <?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.sumAndDifference</methodName>
<params>
<param><value><i4>5</i4></value></param>
<param><value><i4>7</i4></value></param>
</params>
</methodCall>
respon data-> 
 <?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param><value><struct>
<member><name>sum</name>
<value><i4>8</i4></value></member>
<member><name>difference</name>
<value><i4>2</i4></value></member>
</struct></value></param>
</params>
 /methodResponse>
Sum: 8, Difference: 2
幸运的是,确实如此。 现在这是第一个完成的问题。 但第二个问题没有得到答复,有人想回答左边的问题吗?

答案 1 :(得分:0)

好的capede,i4与整数相同。它表示类型,在这种情况下对应于整数。 参考:http://www.tutorialspoint.com/xml-rpc/xml_rpc_data_model.htm

贝斯茨, 卢乔。