请求网络数据JSON

同步请求数据

@interface YcwViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *infoLabel;
- (IBAction)synchronousAction:(id)sender;
@end


- (IBAction)synchronousAction:(id)sender {
    
    
    NSURL *url = [NSURL URLWithString:kWeatherForBeijingAPI];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
    [request setURL:url];
    [request setHTTPMethod:@"GET"];
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (data == nil) {
        NSLog(@"fail !");
        return ;
    }
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSLog(@"%@",dic);  //打印json数据观察结果,方便在下面解析json数据

    //解析json数据
    NSDictionary *weatherDic = [dic objectForKey:@"weatherinfo"];
    
    self.infoLabel.text = [NSString stringWithFormat:@"%@ %@度 %@ %@",[weatherDic objectForKey:@"city"],[weatherDic objectForKey:@"temp"],[weatherDic objectForKey:@"WD"],[weatherDic objectForKey:@"WS"]];
    
    
}


异步请求数据

@interface YcwViewController : UIViewController<NSURLConnectionDataDelegate>
@property (strong, nonatomic) IBOutlet UILabel *infoLabel;
- (IBAction)asynchronousAction:(id)sender;

@end


- (IBAction)asynchronousAction:(id)sender {
    //显示网络请求指示符
    if (![UIApplication sharedApplication ].networkActivityIndicatorVisible) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    }
    
    NSURL *url = [NSURL URLWithString:kWeatherForBeijingAPI];
    //根据NSURL创建请求(此处为可扩展请求)
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
   

    //发送异步请求建立连接,同时设置委托对象,异步请求其实是在主线程之外,开启另一个线程执行这项操作
    [NSURLConnection connectionWithRequest:request delegate:self];
    
    
}


#pragma mark - 请求成功,接收返回数据,自动调用这一个方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

  //获取json字符串
   NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@",json);


    //解析JSON
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    NSLog(@"%@",dic);
    NSDictionary *dicInfo = [dic objectForKey:@"weatherinfo"];
    
    self.infoLabel.text = [NSString stringWithFormat:@"城市:%@,%@摄氏度 %@ %@",[dicInfo objectForKey:@"city"],[dicInfo objectForKey:@"temp"],[dicInfo objectForKey:@"WD"],[dicInfo objectForKey:@"WS"]];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}


#pragma mark - 数据请求失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无法获取数据,请检查网络" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [connection cancel];
}


异步请求数据(block)

- (IBAction)blockAction:(id)sender {
    
    NSURL *url = [NSURL URLWithString:kWeatherForBeijingAPI];
    
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError != nil) {
            NSLog(@"请求失败,%@",connectionError.localizedDescription);
        }
        else{
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@",dic);
            
            NSDictionary *weatherDic = [dic objectForKey:@"weatherinfo"];
            self.infoLabel.text = [NSString stringWithFormat:@"%@ %@度 %@ %@",[weatherDic objectForKey:@"city"],[weatherDic objectForKey:@"temp"],[weatherDic objectForKey:@"WD"],[weatherDic objectForKey:@"WS"]];
        
        }
    }];
    
    
}




你可能感兴趣的:(请求网络数据JSON)