委托NSURLConnection的方法

时间:2014-02-14 09:58:52

标签: ios objective-c json cocoa-touch

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [self.responseData setLength:0];(URL1)
    self.jsonData = [[NSMutableData alloc]init];(URL2)
    self.genderData = [[NSMutableData alloc]init];(URL3)
}

我想一次发送多个url接收响应的进程..?

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


}

这是另一个委托方法

1 个答案:

答案 0 :(得分:0)

使用NSURLConnection时,我倾向于采用以下两种方式之一。

  1. 始终为每个代理使用不同的类。这样,委托实例只需要担心1个URL。

  2. 使用[[connection originalRequest] URL]区分不同的网址。

  3. 示例:

    - (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/
        }
    }