iOS 关于post异步请求

来源:互联网 发布:长沙开福网络花店 编辑:程序博客网 时间:2024/06/06 01:04

 



-(void)loadData
{
    [self showhudView];
    hud = [[JGProgressHUD alloc]initWithStyle:JGProgressHUDStyleDark];
    hud.textLabel.text = @"正在加载......";
    [hud showInView:_hudView animated:YES];

    [self GetIP];

//第一步,创建url

    if ([IPString isEqualToString:@"192.168.33"]) {
       url = [NSURL URLWithString:@"http://192.168.33.203:8090/3dschool/Ajax/getquestion.ashx"];
    }else
    {
//        url = [NSURL URLWithString:@"http://183.195.133.63:8090/3dschool/Ajax/getquestion.ashx"];

         url = [NSURL URLWithString:@"http://q-map.com.cn:8280/3dschool/Ajax/getquestion.ashx"];
//        url = [NSURL URLWithString:@"http://192.168.33.203:8090/3dschool/Ajax/getquestion.ashx"];
    }
    //第二步,创建请求
    //第二步,通过URL创建网络请求
    //NSURLRequest初始化方法第一个参数:请求访问路径,第二个参数:缓存协议,第三个参数:网络请求超时时间(秒)
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

   

//其中缓存协议是个枚举类型包含:

    //NSURLRequestUseProtocolCachePolicy//(基础策略)

    //NSURLRequestReloadIgnoringLocalCacheData//(忽略本地缓存)

    //NSURLRequestReturnCacheDataElseLoad//(首先使用缓存,如果没有本地缓存,才从原地址下载)

    //NSURLRequestReturnCacheDataDontLoad//(使用本地缓存,从不下载,如果本地没有缓存,则请求失败,此策略多用于离线操作) www.2cto.com

    //NSURLRequestReloadIgnoringLocalAndRemoteCacheData//(无视任何缓存策略,无论是本地的还是远程的,总是从原地址重新下载)

    //NSURLRequestReloadRevalidatingCacheData//(如果本地缓存是有效的则不下载,其他任何情况都从原地址重新下载)

   





    [request setHTTPMethod:@"POST"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];

}




#pragma mark - 获取当前网络ip地址
-(void)GetIP{
    NSString *String = [NSString deviceIPAdress];
    if ([String isEqualToString:@"an error occurred when obtaining ip address"]) {
//        UIAlertView * alert3 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的网络连接异常,请检查您的网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"关  闭", nil];
//        [alert3 show];
    }else
    {
        NSArray *IPArray = [String componentsSeparatedByString:@"."];
        IPString = [NSString stringWithFormat:@"%@.%@.%@",IPArray[0],IPArray[1],IPArray[2]];
}

}







- (void)showhudView
{
    _hudView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50)];
    
    _hudView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_hudView];
    
}


-(void)RemoveCover
{
    [hud dismissAnimated:YES];
    if (_hudView) {
        [UIView animateWithDuration:0.5 animations:^{
            _hudView.alpha = 0;
        } completion:^(BOOL finished) {
            _hudView = nil;
        }];
        
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{
    
    _Data = [[NSMutableData alloc]init];
    
    
}
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
    
    [_Data appendData:data];
    
}

//数据传完之后调用此方法

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    
    id jsonData = [NSJSONSerialization JSONObjectWithData:_Data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = jsonData;
    for (NSDictionary *d in array)
    {
        
        _model = [[TestModels alloc]init];
        [_model setValuesForKeysWithDictionary:d];
        
        if (_model.QName != nil && _model.QDesc != nil && _model.QScore != nil && _model.QCode != nil) {
            [_dataArray addObject:_model.QName];
            [_QDescArray addObject:_model.QDesc];
            [_QNameArray addObject:_model.QName];
            [_IDArray addObject:_model.ID];
            [_QTimeArray addObject:_model.QTime];
            [_QCodeArray addObject:_model.QCode];
            [_QScoreArray addObject:_model.QScore];
        }
        
    }
    [self performSelector:@selector(RemoveCover) withObject:nil afterDelay:1.0];
    [self promptView];
}


//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法

-(void)connection:(NSURLConnection *)connection

 didFailWithError:(NSError *)error

{
    NSString *str = [NSString stringWithFormat:@"%@",error.localizedDescription];
//    NSLog(@"%@",error.localizedDescription);
    UIAlertView * alert0 = [[UIAlertView alloc] initWithTitle:nil message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"关  闭", nil];
    [alert0 show];
    [self RemoveCover];
    [self promptView];
}

1 0