哪个是从JSON url获取数据的最佳方法?

时间:2014-09-15 12:58:21

标签: json uitableview ios7

我有一个应用程序,当我们登录时,将加载一个新的视图控制器,这是一个表视图,需要通过Json调用从服务器获取数据。这是在这种情况下使用的最佳方法。

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


    }




}

1 个答案:

答案 0 :(得分:1)

如果以异步方式执行,则该过程将在后台完成。 简而言之,同步请求阻止了代码的执行,这会产生"冻结"在屏幕上和无响应的用户体验。

检查以下网址,可能会让您更清楚。

Synchronous and Asynchronous Requests