从对象数组(id)创建整数数组并发送到Web服务

时间:2013-07-06 17:50:43

标签: iphone ios arrays web-services soap

我正在创建一个iOS应用,需要连接到“http://xxx.xxx.xx.xxx/WebSiteServices.svc?wsdl”等网络服务。

应用程序允许在选择一些字段后为带行李的旅行报价:

  • 原产国(原产国名单);

  • 目的地国家/目的地国家/地区列表;

  • 5个行李箱的5个识别字段,每个行李允许选择不同身份证的行李数量。

为了与Web服务进行通信,我进行了SOAP调用,如链接中所述:“iPhone interaction with ASP.NET WebService。”

我成功接收了国家和行李列表,现在我无法将所选数据发送到Web服务以调用“calcolaImporto”(计算金额)方法。我必须发送SOAP消息:

  • idPaesePrelievo:原籍国的ID(整数:好的,我成功了);

  • idPaeseDest:目的地国家/地区的ID(整数:好的,我成功了);

  • idProdotti:标识所选存储的ID的整数列表(问题:我无法发送数组);

  • qtaProdotti:标识为每个第一个列表选择的行李ID数量的整数列表(问题:我无法发送数组)。

这两个列表没有相互连接,但是我无法将这两个数组发送到Web服务。

Web服务中的数组由两个整数列表组成,即使两个Xcode数组由两个对象id列表组成(我也尝试从id转换为int,但没有)。

访问该方法,但结果为“0”,因为没有检查任何行李:我该怎么办?

请帮助我,谢谢!

下面我发布了“ViewController.m”的代码:

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize amount, idPaeseDest, idPaesePrelievo, idProdotti, qtaProdotti;
/* amount (TEXTFIELD), idPaeseDest (INT), idPaesePrelievo (INT), idProdotti (NSMUTABLEARRAY), qtaProdotti (NSMUTABLEARRAY) */

- (void)viewDidLoad {
[super viewDidLoad];
}

- (IBAction)calcolaImporto:(id)sender {
// Create the SOAP message
NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                     "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                     "<soap:Body>"
                     "<CalcolaImporto xmlns=\"http://tempuri.org/\">"
                     "<idPaesePrelievo>%d</idPaesePrelievo>"
                     "<idPaeseDest>%d</idPaeseDest>"
                     "<idProdotti>%@</idProdotti>"
                     "<qtaProdotti>%@</qtaProdotti>"
                     "</CalcolaImporto>"
                     "</soap:Body>"
                     "</soap:Envelope>", idPaesePrelievo, idPaeseDest, idProdotti, qtaProdotti];
// Create the URL
NSURL *url = [NSURL URLWithString: @"http://xxx.xxx.xx.xxx/WebSiteServices.svc?wsdl"];
// Create the request
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/IBagExpressServices/CalcolaImporto" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [[NSMutableData data] retain];
}
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response {
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
[webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error {
UIAlertView *errore = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Connection problem" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errore show];
[errore release];
[webData release];
[connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSLog(@"Ok. Byte: \n %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"theXML: \n %@", theXML);
[theXML release];
if (xmlParser) {
    [xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
[webData release];
}

-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict {
if ([elementName isEqualToString:@"CalcolaImportoResponse"])
    soapResults = [[NSMutableString alloc] init];
elementFound = YES;
}

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
if (elementFound)
    [soapResults appendString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if ([elementName isEqualToString:@"CalcolaImportoResult"])
    amount.text=soapResults;

elementFound = FALSE;
}

- (void)dealloc {
[amount release];
[soapResults release];
[super dealloc];
}
@end

2 个答案:

答案 0 :(得分:0)

你不能用整数列表创建一个NSString并用,分隔它们并发送它吗?

NSMutableString *mutableList = [[NSMutableString alloc ]initWithString:@"<q14:ArrayOfint>"];
//I'll say like this because i don't know if you added them as NSNumber or NSString
for(int i = 0 ;i < idProdotti.count;i++)
{
NSInteger value = [[idProdotti objectAtIndex:i]integerValue];

  [mutableList appendFormat:@"<arr:int>%d</arr:int>",value];

 }
[mutableList appendFormat:@"</q14:ArrayOfint>"];

然后发送mutableList,然后发送release; // qtaProdotti的第二个

NSMutableString *mutableList1 = [[NSMutableString alloc ]initWithString:@"<q15:ArrayOfint>"];
//I'll say like this because i don't know if you added them as NSNumber or NSString
for(int i = 0 ;i < qtaProdotti.count;i++)
{
NSInteger value = [[qtaProdotti objectAtIndex:i]integerValue];

  [mutableList1 appendFormat:@"<arr:int>%d</arr:int>",value];

 }
[mutableList1 appendFormat:@"</q15:ArrayOfint>"];

然后发送mutableList1,然后发送release;

答案 1 :(得分:0)

我这样解决了:

  • 我使用了“SoapUI”,一个与Web服务交互的图形界面,我重写了SOAP消息;

  • 我使用了两个类似于向我展示“Soryngod”的列表。

这是正确的代码:

NSMutableString *idLista = [[NSMutableString alloc] init];
for(int i=0; i<idProdotti.count; i++) {
    NSInteger value = [[idProdotti objectAtIndex:i] integerValue];
    [idLista appendFormat:@"<arr:int>%d</arr:int>",value];
}

NSMutableString *qtaLista = [[NSMutableString alloc] init];
for(int i=0; i<qtaProdotti.count; i++) {
    NSInteger value = [[qtaProdotti objectAtIndex:i] integerValue];
    [qtaLista appendFormat:@"<arr:int>%d</arr:int>",value];
}

NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                 "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem="http://tempuri.org/\" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays\">"
                 "<soapenv:Body>"
                 "<tem:CalcolaImporto>"
                 "<tem:idPaesePrelievo>%d</tem:idPaesePrelievo>"
                 "<tem:idPaeseDest>%d</tem:idPaeseDest>"
                 "<tem:idProdotti>%@</tem:idProdotti>"
                 "<tem:qtaProdotti>%@</tem:qtaProdotti>"
                 "</tem:CalcolaImporto>"
                 "</soapenv:Body>"
                 "</soapenv:Envelope>", idPaesePrelievo, idPaeseDest, idLista, qtaLista];

[...]