在Soap Request ios中传递数组

时间:2014-04-25 10:21:39

标签: ios objective-c soap

我想请求Soap,如下面的

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:SetEmployeeManager>
         <!--Optional:-->
         <tem:sEmployeeMapId>
            <!--Zero or more repetitions:-->
            <tem:string>2</tem:string>
        <tem:string>3</tem:string>
         </tem:sEmployeeMapId>
         <!--Optional:-->

         <tem:sFlage>
            <!--Zero or more repetitions:-->
            <tem:string>true</tem:string>
            <tem:string>false</tem:string>
         </tem:sFlage>
      </tem:SetEmployeeManager>
   </soapenv:Body>
</soapenv:Envelope>

以下是我的代码已完成但未被称为此服务。

webServiceHelper.MethodName = WS_SETMANAGER;
    webServiceHelper.MethodParameters = [[NSMutableDictionary alloc] init];
    webServiceHelper.MethodSubParameters = [[NSMutableDictionary alloc] init];
    [webServiceHelper.MethodParameters setValue:@"" forKey:@"sEmployeeMapId"];
    [webServiceHelper.MethodParameters setValue:@"" forKey:@"sFlage"];
//    NSString *strEmpId;
    for (int i=0; i<[[dict valueForKey:@"empId"]count]; i++)
    {
         [webServiceHelper.MethodSubParameters setValue:[[dict valueForKey:@"empId"] objectAtIndex:i]  forKey:@"long"];
//        [webServiceHelper.MethodSubParameters setValue:[[dict valueForKey:@"flag"] objectAtIndex:i]  forKey:@"string"];
    }
    for (int i=0; i<[[dict valueForKey:@"flag"]count]; i++)
    {
        [webServiceHelper.MethodSubParameters setValue:[[dict valueForKey:@"flag"] objectAtIndex:i]  forKey:@"string"];
    }

和请求代码是

NSMutableString *sRequest = [[NSMutableString alloc] init];
    self.SOAPActionURL = [NSString stringWithFormat:@"%@%@%@",self.XMLNameSpace, slashUsed, self.MethodName];

    //make soap request
    //  [sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
    [sRequest appendString:@"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">\n"];
    [sRequest appendString:@"<soap:Header/>\n"];
    [sRequest appendString:@"<soap:Body>\n"];
    [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>",MethodName]];
    if(MethodParametersAsString != nil) [sRequest appendString:MethodParametersAsString];

    NSEnumerator *tableIterator = [MethodParameters keyEnumerator];
    NSEnumerator *tableSubIterator = [MethodSubParameters keyEnumerator];
    NSString *keyID;
    NSString *subKeyId;
    while(keyID = [tableIterator nextObject])
    {
//        [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>%@</tem:%@>\n", keyID, [MethodParameters objectForKey:keyID], keyID]];
//        [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>%@</tem:%@>\n", keyID, [MethodSubParameters objectForKey:keyID], keyID]];
        [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>",keyID]];
        while (subKeyId = [tableSubIterator nextObject])
        {
            [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>%@</tem:%@>\n", subKeyId,[MethodSubParameters objectForKey:subKeyId], subKeyId]];
        }
        [sRequest appendString:[NSString stringWithFormat:@"</tem:%@>",keyID]];
    }

    //close envelope
    [sRequest appendString:[NSString stringWithFormat:@"</tem:%@>\n", MethodName]];
    [sRequest appendString:@"</soap:Body>\n"];
    [sRequest appendString:@"</soap:Envelope>\n"];

请帮忙解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

   NSString *strsoap = @"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
                     "<soapenv:Header/>"
                     "<soapenv:Body>"
                     "<tem:SetEmployeeManager>"
                     "<tem:sEmployeeMapId>"
                     "<tem:string>2</tem:string>"
                     "<tem:string>3</tem:string>"
                     "</tem:sEmployeeMapId>"
                     "<tem:sFlage>"
                     "<tem:string>true</tem:string>"
                     "<tem:string>false</tem:string>"
                     "</tem:sFlage>"
                     "</tem:SetEmployeeManager>"
                     "</soapenv:Body>"
                     "</soapenv:Envelope>";

    [yourequest setHTTPBody:[strsoap dataUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:1)

for (int i=0; i<[[dict valueForKey:@"empId"]count]; i++)
{
    [webServiceHelper.MethodSubParameters setValue:[[dict valueForKey:@"empId"] objectAtIndex:i]  forKey:@"long"];
}

此处在每次迭代中MethodSubParameters都会重写值forKey:@"long"。相反,试试这个,

webServiceHelper.MethodName = WS_SETMANAGER;
webServiceHelper.MethodParameters = [[NSMutableDictionary alloc] init];
NSMutableDictionary empIdSubDict = [[NSMutableDictionary alloc] init];
[empIdSubDict setObject:[dict valueForKey:@"empId"] forKey:@"long"];
[webServiceHelper.MethodParameters setObject:empIdSubDict forKey:@"sEmployeeMapId"];

NSMutableDictionary flagSubDict = [[NSMutableDictionary alloc] init];
[flagSubDict setObject:[dict valueForKey:@"flag"] forKey:@"string"];
[webServiceHelper.MethodParameters setObject:flagSubDict forKey:@"sFlage"];

请求代码

.....
NSEnumerator *tableIterator = [MethodParameters keyEnumerator];
NSString *keyID;
while(keyID = [tableIterator nextObject])
{
    [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>",keyID]];
    if ([[MethodParameters objectForKey:keyID] isKindOfClass:[NSDictionary class]]) {
        NSDictionary *subParams = [MethodParameters objectForKey:keyID];
        NSEnumerator *tableSubIterator = [subParams keyEnumerator];
        NSString *subKeyId;
        while (subKeyId = [tableSubIterator nextObject]) {
            if ([[MethodParameters objectForKey:subKeyId] isKindOfClass:[NSArray class]]) {
                NSArray *subValues = [MethodParameters objectForKey:subKeyId];
                for (NSString *subValue in subValues) {
                   [sRequest appendString:[NSString stringWithFormat:@"<tem:%@>%@</tem:%@>\n", subKeyId,subValue, subKeyId]];
                }
            }
        }
    }
    [sRequest appendString:[NSString stringWithFormat:@"</tem:%@>",keyID]];
}
....