0

我正在创建一个需要连接到 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
4

2 回答 2

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];

[...]
于 2013-07-10T11:17:44.610 回答
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它;

于 2013-07-06T17:58:11.963 回答