zjjzmw1 发表于 2013-1-30 20:22:20

NSURLConnection的用法

NSURLRequest *request = ];
NSURLConnection *topAppsConnection = [[ initWithRequest:request delegate:self] autorelease];
if (topAppsConnection)
    self.topAppsData = ;
下面是NSURLConnection的几个代理方法:
#pragma mark NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSInteger status = [(NSHTTPURLResponse *)response statusCode];
   
    if (status != 200)
      ;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    ;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    ;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *topAppsString = [[ initWithData:topAppsData encoding:NSUTF8StringEncoding] autorelease];
    self.topAppsData = nil;
   
    @try {
      RootViewController *rootViewController = (RootViewController *);
      rootViewController.topApps = [[ objectForKey:@"feed"]objectForKey:@"entry"];
      ;
    }
    @catch (NSException *e) {
      
    }
}




接下来,就是这几个代理方法中用到的cancel:函数的实现:

- (void)cancel:(NSURLConnection *)connection {
    ;
    self.topAppsData = nil;
}
页: [1]
查看完整版本: NSURLConnection的用法