【iOS开发-网络】使用get请求发送和接受数据

来源:互联网 发布:奥林巴斯显微镜 知乎 编辑:程序博客网 时间:2024/06/10 01:32

发送网络请求要使用异步的方式,不能使用同步的方式

并且异步的get请求有两种方式

第一种

使用 sendAsynchronousRequest 方法实现

    //设置请求路径    NSString *urlStr = [NSString stringWithFormat:@"http://172.16.20.107:8080/TFServer/login?username=%@&pwd=%@", username, password];    //按照utf8编码 转成没有中文的字符串    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    //url不能包含中文    NSURL *url = [NSURL URLWithString:urlStr];    //创建请求对象,默认就是get请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    //创建一个队列,因为界面刷新的操作只能在主线程,所以这个队列一般就是主线程队列    NSOperationQueue *queue = [NSOperationQueue mainQueue];    //开始发送异步请求,其中data为服务器端返回的所有数据,代码块为请求结束所执行的事    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {        [MBProgressHUD hideHUD];//隐藏正在登录的那个框框        if(data) {            //将返回的json数据解析为字典            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];            NSString *error = dict[@"error"];            if(error) {                [MBProgressHUD showError:@"网络繁忙"];            } else {                [MBProgressHUD showSuccess:@"登录成功"];            }        }    }];

第二种

第二种方法是使用代理方法,这个也是异步请求,当请求开始其他线程就会执行代理方法

开始一个网络请求

//也是一个异步的 NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];//请求开始[conn start];

四个常用的代理方法

//收到服务器端的回应()请求成功 第一执行-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {}//服务器端返回数据就会调用这个方法,可能会调用多次,因为每次只返回一部分数据 第二执行-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {}//接受玩数据会调用 最后执行-(void)connectionDidFinishLoading:(NSURLConnection *)connection {}

完整代码

    //设置请求路径    NSString *urlStr = [NSString stringWithFormat:@"http://172.21.190.87:8080/TFServer/login?username=%@&pwd=%@", username, password];    //按照utf8编码 转成没有中文的字符串    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    //url不能包含中文    NSURL *url = [NSURL URLWithString:urlStr];    //创建请求对象,默认就是get请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    request.timeoutInterval = 5;//设置请求超时时间//直接就发出一个异步请求    [NSURLConnection connectionWithRequest:request delegate:self];}#pragma mark -实现代理方法//请求错误或者失败的时候调用(断网)请求失败会执行这个-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {    NSLog(@"didFailWithError");    [MBProgressHUD showError:@"请求失败,请检查您的网络"];}//收到服务器端的回应()请求成功 第一执行-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {    NSLog(@"didReceiveResponse");    self.responseData = [NSMutableData data];}//服务器端返回数据就会调用这个方法,可能会调用多次,因为每次只返回一部分数据 第二执行-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {    NSLog(@"didReceiveData");    NSLog(@"%@", data);    [self.responseData appendData:data];     NSLog(@"%@", self.responseData);}//接受玩数据会调用 最后执行-(void)connectionDidFinishLoading:(NSURLConnection *)connection {    NSLog(@"connectionDidFinishLoading");    [MBProgressHUD hideHUD];    NSLog(@"%@", self.responseData);    //将返回的json数据解析为字典    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableContainers error:nil];    NSString *error = dict[@"error"];    if(error) {        [MBProgressHUD showError:@"网络繁忙"];    } else {        [MBProgressHUD showSuccess:@"登录成功"];    }}
0 0
原创粉丝点击