[英]Delegate Methods of NSURLConnection
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.responseData setLength:0];(URL1)
self.jsonData = [[NSMutableData alloc]init];(URL2)
self.genderData = [[NSMutableData alloc]init];(URL3)
}
我想一次發送多個網址,以什么方式接收響應..?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
NSError * error;
id result = (NSMutableArray *)[NSJSONSerialization JSONObjectWithData:self.jsonData options:kNilOptions error:&error];
if (error)
{
NSLog(@"DATA LOAD ERROR");
}
else
{
if([result isKindOfClass:[NSArray class]])
{
titlesArray = result;
}
else
{
titlesDic = result;
}
}
[self genderURLMethod];
}
這是另一種委托方法
使用NSURLConnection
,我傾向於采用以下兩種方法之一。
始終為每個委托使用不同的類。 這樣,委托實例只需要擔心1個URL。
使用[[connection originalRequest] URL]
區分不同的URL。
例:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *URLString = [[[[connection originalRequest] URL] absoluteString];
if ([URLString isEqualToString:MY_URL_1]) {
// Handle the response for the first URL.
} else if ([URLString isEqualToString:MY_URL_2]) {
// Handle the response for the second URL/
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.