试试 NSURLCONNECTION 这将帮助你调用异步 web 服务
NSURLConnection 对象用于使用 HTTP 执行 Web 服务。使用 NSURLConnection 时,请求以异步形式发出。这意味着您无需等待请求结束即可继续,
该委托必须实现以下方法:
connection:didReceiveResponse : called after the connection is made successfully and before receiving any data. Can be called more than one time in case of redirection.
connection:didReceiveData : called for each bloc of data.
connectionDidFinishLoading : called only one time upon the completion of the request, if no error.
connection:didFailWithError : called on error.
例子: -
NSData *data = [[NSMutableData alloc] init];
NSURL *url_string = [NSURL URLWithString:
@"Your URL"];
NSURLRequest *request = [NSURLRequest requestWithURL:url_string];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (!conn) {
// this is better if you @throw an exception here
NSLog(@"error while starting the connection");
[data release];
}
对于收到的每个原始数据块,您可以在此方法中附加您的数据:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData {
[data appendData:someData];
}
connectionDidFinishLoading
将在成功接收数据结束时调用