通过一个简单的联网获取JSON数据并将其解析
举个例子 获取伦敦的天气数据
NSString*londonWeatherUrl =@"http://api.openweathermap.org/data/2.5/weather?q=London,uk";
//这里是使用NSURLConnection联网的方法
NSURLRequest*request = [NSURLRequestrequestWithURL:[NSURLURLWithString:londonWeatherUrl]];
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*response,
NSData*data,
NSError*connectionError) {// handle response
}]
//通过NSURLSession联网的方法
NSURLSession*session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURLURLWithString:londonWeatherUrl]
completionHandler:^(NSData*data,NSURLResponse*response,
NSError*error) {
// handle response
}] resume];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request,NSHTTPURLResponse *response,
id JSON) {// handle response
} failure:nil];[operation start];