本文共 5839 字,大约阅读时间需要 19 分钟。
这周写天气预报时需要网络请求才开始正式接触,理解的可能不够全面,有什么不对的地方,敬请斧正
这篇以搜索城市时的联想为例,即
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
里使用了创建URL方法- stringByAddingPercentEncodingWithAllowedCharacters:
- sessionWithConfiguration: delegate: delegateQueue:
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
-(void)URLSession:(NSURLSession *)session dataTask:( NSURLSessionDataTask *)dataTask didReceiveData:( NSData *)data
-(void)URLSession:(NSURLSession *)session task:( NSURLSessionTask *)task didCompleteWithError:( NSError *)error
- JSONObjectWithData: options: error:
解析数据时使用的方法(使用 NSDictionary 接收) - [[NSOperationQueue mainQueue] addOperationWithBlock:
首先在 ViewController.h 里声明一些属性及协议
@interface ViewController : UIViewController@property (nonatomic, strong) UITextField *textField;@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSMutableData *data;@property (nonatomic, strong) NSMutableArray *cityArray;
在 ViewController.m 里初始化UITextField和UITableView
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _textField = [[UITextField alloc] init]; _textField.delegate = self; _textField.frame = CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width - 100, 50); [self.view addSubview:_textField]; _textField.borderStyle = UITextBorderStyleRoundedRect; _textField.keyboardType = UIKeyboardTypeDefault; [self creatTableView]; }- (void)creatTableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 150, [UIScreen mainScreen].bounds.size.width - 100, 500) style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"111"]; UIView *view = [[UIView alloc] init]; _tableView.tableFooterView = view; }
然后创建URL及协议方法
- (void)creatUrl { //1.创建请求地址 NSString *urlString = [NSString stringWithFormat:@"https://search.heweather.com/find?location=%@&key=71f533964a994355bb1abfccb161d241", _textField.text]; //处理字符 urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //创建URL NSURL *url = [NSURL URLWithString:urlString]; NSLog(@"%@", urlString); //2.创建请求类 NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.创建会话 //delegateQueue 表示协议方法在哪个线程中执行 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; //4.根据会话创建任务 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; //5.启动任务 [dataTask resume]; }
URL协议方法:
//接收服务器的响应-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler { NSLog(@"didReceiveResponse"); if(self.data == nil){ self.data = [[NSMutableData alloc] init]; } else { self.data.length = 0; } //允许接收数据 completionHandler(NSURLSessionResponseAllow);}//接收到数据,该方法会被调用多次- (void)URLSession:(NSURLSession *)session dataTask:( NSURLSessionDataTask *)dataTask didReceiveData:( NSData *)data { NSLog(@"didReceiveData"); [self.data appendData:data];}//数据请求完成或者请求出现错误调用的方法- (void)URLSession:(NSURLSession *)session task:( NSURLSessionTask *)task didCompleteWithError:( NSError *)error { NSLog(@"didCompleteWithError"); if (error == nil) { //解析数据 NSDictionary *secondDictionary = [NSJSONSerialization JSONObjectWithData:_data options:kNilOptions error:nil]; NSMutableArray *timeArray = [[NSMutableArray alloc] init]; timeArray = secondDictionary[@"HeWeather6"][0][@"basic"]; for (int i = 0; i < timeArray.count; i++) { NSMutableString *str = [NSMutableString stringWithFormat:@"%@-%@",timeArray[i][@"parent_city"],timeArray[i][@"location"]]; [_cityArray addObject: str]; } for (int i = 0; i < _cityArray.count; i++) { NSLog(@"%@", _cityArray[i]); } NSLog(@"-----------over"); } else { NSLog(@"error = %@", error); } [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self->_tableView reloadData]; }]; }
UITableView的协议方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath]; while ([cell.contentView.subviews lastObject] != nil) { [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview]; } cell.textLabel.text = _cityArray[indexPath.row]; cell.textLabel.font = [UIFont systemFontOfSize:20]; cell.textLabel.textAlignment = NSTextAlignmentCenter; return cell; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _cityArray.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 30;}- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event { [_textField endEditing:YES];}
转载地址:http://jukti.baihongyu.com/