【问题标题】:application is getting flashed while calling web services in iOS在 iOS 中调用 Web 服务时应用程序正在闪烁
【发布时间】:2013-07-31 18:02:59
【问题描述】:

我正在使用 Soap Web 服务从服务器下载数据。下载数据时设备像闪光灯一样闪烁。我正在使用同步请求来获取数据。 我找不到它闪现的原因。 请帮帮我,在此先感谢。

代码如下:

NSString *msgString = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                               "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " 
                               "xmlns:xsd=\"http://http://www.w3.org/2001/XMLSchema\" " 
                               "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                               "<soap:Body>"
                               "<GetCategories xmlns=\"http://tempuri.org/\"/>"
                               "</soap:Body>"
                               "</soap:Envelope>"]; 
        NSURL *url = [NSURL URLWithString:[DefaultSettings getLink]];
        NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
        //---set the various headers---
        NSString *msgLength = [NSString stringWithFormat:@"%d", [msgString length]];
        [req addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
        [req addValue:@"http://tempuri.org/GetCategories" forHTTPHeaderField:@"SOAPAction"];
        [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

        //---set the HTTP method and body---
        [req setHTTPMethod:@"POST"];
        [req setHTTPBody:[msgString dataUsingEncoding:NSUTF8StringEncoding]];
        NSError *error;
        NSURLResponse *response;
 NSData *webData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
xmlParser = [[NSXMLParser alloc] initWithData:webData];
                [xmlParser setDelegate:self];
                [xmlParser setShouldProcessNamespaces:NO];
                [xmlParser setShouldReportNamespacePrefixes:NO];
                [xmlParser setShouldResolveExternalEntities:YES];
                [xmlParser parse];

在此之后,我正在解析数据。同样的方法,我一一调用了8个soap服务。

【问题讨论】:

  • 请贴一些代码!否则几乎不可能确定问题。
  • 请看代码,我已发送代码

标签: objective-c web-services


【解决方案1】:

这是因为你正在使用 [NSURLConnection sendSynchronousRequest:req returningResponse:&amp;response error:&amp;error];。 此方法触发同步连接并阻塞线程,直到您从服务器获得响应。

你可以使用 - (id)initWithRequest:(NSURLRequest *)request delegate:(id &lt; NSURLConnectionDelegate &gt;)delegate startImmediately:(BOOL)startImmediately 使用 NSThread 启动异步连接或从另一个线程启动以前的方法

如果您的应用执行多个请求,您应该创建一个 RequestManager 来执行所有请求。该管理器将实现NSURLConnectionDelegate。然后创建您自己的协议并在您的 ViewController 子类中实现它。

例如,在您的协议中,您可以添加一个名为

的方法
- (void)request:(int)requestID didFinishWithResult:(NSData *)data

requestID 用于识别您的请求。

当任何请求完成时调用此方法。

【讨论】:

  • 感谢您的回复,但是在这里我需要为许多类实现 NSURLConnection 委托方法。异步调用服务没有从模态类调用委托方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-25
相关资源
最近更新 更多