我正在创建一个需要连接到 Web 服务的应用程序 iOS,例如“ http://xxx.xxx.xx.xxx/WebSiteServices.svc?wsdl ”。
该应用程序允许在选择一些字段后为带行李的旅行报价:
原产国(原产国列表);
目的地国家(目的地国家列表);
5个识别字段,对应5个id的行李,每个字段可以选择不同id的行李数量。
为了与 Web 服务通信,我按照以下链接中的说明进行了 SOAP 调用:“ iPhone 与 ASP.NET WebService 的交互”。
我成功接收了国家和行李清单,现在我无法将所选数据发送到 Web 服务以调用“calcolaImporto”(计算金额)方法。我必须发送 SOAP 消息:
idPaesePrelievo:原产国的ID(整数:OK,我成功了);
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