码迷,mamicode.com
首页 > 其他好文 > 详细

AFN同步异步请求

时间:2017-04-12 11:18:48      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:tab   error:   生态   cal   range   undle   version   amp   出错   

异步请求:

-(BOOL)getOnlyKey1
{
    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    __block bool isTrue = false;
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
    NSURL *url = [NSURL URLWithString:urlstr];
    NSDictionary *dic = @{@"imei":myUUIDStr,@"av":AppVersion};
    [manager POST:urlstr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyLog(@"%@", operation.responseString);
        NSRange range = [operation.responseString rangeOfString:@"\"msg\":\"0\""];
        if (range.location != NSNotFound) {
            isTrue = true;
        }
        if (!isTrue) {
            SHOWALERT(@"错误", @"您须要联系开发者");
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyLog(@"返回失败结果:%@", error.localizedFailureReason);
        SHOWALERT(@"错误", @"请求开发者server失败");
        isTrue = true;
    }];
    return  isTrue;
}

同步请求:

-(BOOL)getOnlyKey2
{
    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    BOOL isTrue = false;
    NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
    NSURL *url = [NSURL URLWithString:urlstr];
    NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url];
    urlrequest.HTTPMethod = @"POST";
    NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, AppVersion];
    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    urlrequest.HTTPBody = body;
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];
    requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [requestOperation start];
    [requestOperation waitUntilFinished];
    MyLog(@"%@",requestOperation.responseString);
    NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""];
    if (range.location != NSNotFound) {
        isTrue = true;
    }
    if (!isTrue) {
        SHOWALERT(@"错误", @"您须要联系开发者");
    }
    return  isTrue;
}

原生态的同步请求:

-(BOOL)getOnlyKey
{
    NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    
    //应用版本
    NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
    NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
    
    
    NSString *urlString = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, versionNum];
    //将nstring转换成nsdata
    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    //MyLog(@"body data %@", body);
    [request setHTTPBody:body];
    NSURLResponse *response = nil;
    NSError *error = nil;
    //第二,三个參数是指针的指针,全部要用取址符,这种方法是同步方法。同步操作没有完毕。后面的代码不会运行。
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    //    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    //    MyLog(@"返回结果是:%@", str);
    
    if (error == nil) {  //接受到数据,表示工作正常
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        MyLog(@"%@",str);
        NSRange range = [str rangeOfString:@"\"msg\":\"0\""];
        if (range.location != NSNotFound) {
            return true;
        }else{
            return false;
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错鸟"
                                                            message:@"您须要联系项目开发者"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }
    
    if(error != nil || response == nil)
    {
        return false;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误"
                                                        message:@"登陆失败。网络不稳定"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        
        
    }
    
    return false;
}


AFN同步异步请求

标签:tab   error:   生态   cal   range   undle   version   amp   出错   

原文地址:http://www.cnblogs.com/brucemengbm/p/6698024.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!