У меня есть приложение, в котором при входе в систему будет загружен новый контроллер представления, который является табличным представлением, и данные должны быть получены с сервера по вызову Json. Это лучший метод для использования в этой ситуации.Каков наилучший метод для извлечения данных из JSON-url?
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://YourURL.com/FakeURL/PARAMETERS"]];
[request setHTTPMethod:@"GET"];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
или
-(void)update{
NSString *urlAsString = [NSString stringWithFormat:@"http://YourURL.com/FakeURL/PARAMETERS"];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSString *errorDisplay = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
NSLog(@"%@",errorDisplay);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
}
Или я должен использовать асинхронный метод
-(void)updateCheckList{
self.checkListresponseData = [NSMutableData data] ;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/outing-service-web/checkList/all"]];//asynchronous call
checkListConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[self.checkListresponseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.checkListresponseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSString *errorDisplay = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
NSLog(@"%@",errorDisplay);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot connect to server" delegate:nil cancelButtonTitle:nil otherButtonTitles: @"Retry",@"Close",nil];
[alertView show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == checkListConn) {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.checkListresponseData length]);
// convert to JSON
NSError *myError = nil;
NSArray *res = [NSJSONSerialization JSONObjectWithData:self.checkListresponseData options:NSJSONReadingMutableLeaves error:&myError];
}
}