0

我需要等待来自 SOAP Web 服务的响应,我通过 NSURLConnection 调用,因为我需要操作返回的数据,然后将其从我的类返回给调用类。

这是我的代码:

#import <Foundation/Foundation.h>


@interface UsersBLL : NSObject {

 NSMutableData *webData;
 NSMutableString *soapResults;
 NSXMLParser *xmlParser;
 BOOL *recordResults;
 NSNumber *EmailCount;
}

@property(nonatomic, retain) NSMutableData *webData;
@property(nonatomic, retain) NSMutableString *soapResults;
@property(nonatomic, retain) NSXMLParser *xmlParser;



-(int)checkEmailAddress:(NSString*)emailAddress;
@end

#import "UsersBLL.h"


@implementation UsersBLL
@synthesize webData;
@synthesize soapResults;
@synthesize xmlParser;

-(id)init {
 self = [super init];
 return self;
}

-(int)checkEmailAddress:(NSString*)emailAddress {
 // Build the SOAP envelope
 NSString *soapMessage = [NSString stringWithFormat:
        @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        "<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/\">\n"
        "<soap:Body>\n"
        "<CheckEmailAddress xmlns=\"http://tempuri.org/\">\n"
        "<EmailAddress>%@</EmailAddress>\n"
        "</CheckEmailAddress>\n"
        "</soap:Body>\n"
        "</soap:Envelope>\n", emailAddress];

 NSLog(soapMessage);

 NSURL *url = [NSURL URLWithString:@"http://photoswapper.mick-walker.co.uk/UsersService.asmx?op=CheckEmailAddress"];
 NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
 NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

 [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
 [theRequest addValue: @"http://tempuri.org/CheckEmailAddress" forHTTPHeaderField:@"SOAPAction"];
 [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
 [theRequest setHTTPMethod:@"POST"];
 [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

 NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

 if( theConnection )
 {
  webData = [[NSMutableData data] retain];
 }
 else
 {
  NSLog(@"theConnection is NULL");
 }
 NSLog(@"%@", EmailCount);
}

-(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
{
 NSLog(@"ERROR with theConenction");
 [connection release];
 [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSLog(@"DONE. Received Bytes: %d", [webData length]);
 NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
 NSLog(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:@"CheckEmailAddressResult"])
 {
  if(!soapResults)
  {
   soapResults = [[NSMutableString alloc] init];
  }
  recordResults = TRUE;
 }
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
 if( recordResults )
 {
  [soapResults appendString: string];
 }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
 if( [elementName isEqualToString:@"CheckEmailAddressResult"])
 {
  recordResults = FALSE;
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  EmailCount = [formatter numberFromString:soapResults];
            [formatter release];
  [soapResults release];
  soapResults = nil;
 }
}

@end

CheckEmailAddress 被声明为返回一个整数值(我知道它在上面的示例中没有返回任何内容)。

我理想中想要的是通过 CheckEmailAddress 方法,返回从 Web 服务检索到的值。但是,由于调用 NSURLConnection 不会等到请求完成,所以我不能这样做。

如果有人能给我任何潜在的解决方法的想法,我将不胜感激。

4

4 回答 4

2

最简单的解决方案是使用[NSURLConnection sendSynchronousRequest:returningResponse:error:].

它不允许像您采用的方法那样多的控制,但通常对于大多数应用程序来说已经足够了。

于 2009-12-13T21:55:58.793 回答
1

我刚刚发布了一个解决方案,它包装了一个异步 NSURLConnection 以便能够阻止调用线程。如果您需要比标准更多的控制,[NSURLConnection sendSynchronousRequest:returningResponse:error:]您可以查看 StackOverflow 上的此链接:

使用信号量实现的 NSURLConnection 阻塞包装器

于 2012-12-05T22:16:19.573 回答
0

你有两个选择:

  • 采用+[NSURLConnection sendSynchronousRequest:returningResponse:error:]

  • 在自定义运行循环模式下安排连接,并在该模式下运行循环,直到数据到达或您需要取消连接

于 2009-12-14T11:14:25.357 回答
0

这完全取决于您需要的异步级别:

  • 如果在整个请求期间保持被阻止是可以的,您可能想要使用

      +[NSURLConnection sendSynchronousRequest:returningResponse:error:]
    

    但是,正如 Wade 所建议的,请小心为您的 NSURLRequest 添加超时,否则连接可能会阻塞并且您的应用程序将挂起。

  • 如果没有,您可以简单地使用 NSNotificationCenter。但是您必须注意数据的竞争条件,特别是在处理多个请求时

于 2010-03-02T10:38:40.697 回答