AFN同步异步请求

来源:互联网 发布:软件定制价格 编辑:程序博客网 时间:2024/05/21 10:20

异步请求:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(BOOL)getOnlyKey1  
  2. {  
  3.     NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];  
  4.       
  5.     __block bool isTrue = false;  
  6.       
  7.     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  8.     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];  
  9.     NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];  
  10.     NSURL *url = [NSURL URLWithString:urlstr];  
  11.     NSDictionary *dic = @{@"imei":myUUIDStr,@"av":AppVersion};  
  12.     [manager POST:urlstr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  13.         MyLog(@"%@", operation.responseString);  
  14.         NSRange range = [operation.responseString rangeOfString:@"\"msg\":\"0\""];  
  15.         if (range.location != NSNotFound) {  
  16.             isTrue = true;  
  17.         }  
  18.         if (!isTrue) {  
  19.             SHOWALERT(@"错误"@"您需要联系开发人员");  
  20.         }  
  21.           
  22.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  23.         MyLog(@"返回失败结果:%@", error.localizedFailureReason);  
  24.         SHOWALERT(@"错误"@"请求开发人员服务器失败");  
  25.         isTrue = true;  
  26.     }];  
  27.     return  isTrue;  
  28. }  

同步请求:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(BOOL)getOnlyKey2  
  2. {  
  3.     NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];  
  4.     BOOL isTrue = false;  
  5.     NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];  
  6.     NSURL *url = [NSURL URLWithString:urlstr];  
  7.     NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url];  
  8.     urlrequest.HTTPMethod = @"POST";  
  9.     NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, AppVersion];  
  10.     NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];  
  11.     urlrequest.HTTPBody = body;  
  12.     AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];  
  13.     requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];  
  14.     [requestOperation start];  
  15.     [requestOperation waitUntilFinished];  
  16.     MyLog(@"%@",requestOperation.responseString);  
  17.     NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""];  
  18.     if (range.location != NSNotFound) {  
  19.         isTrue = true;  
  20.     }  
  21.     if (!isTrue) {  
  22.         SHOWALERT(@"错误"@"您需要联系开发人员");  
  23.     }  
  24.     return  isTrue;  
  25. }  

原生态的同步请求:

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -(BOOL)getOnlyKey  
  2. {  
  3.     NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];  
  4.       
  5.     //应用版本号  
  6.     NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];  
  7.     NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];  
  8.       
  9.       
  10.     NSString *urlString = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];  
  11.     NSURL *url = [NSURL URLWithString:urlString];  
  12.       
  13.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  14.       
  15.     [request setHTTPMethod:@"POST"];  
  16.     NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, versionNum];  
  17.     //将nstring转换成nsdata  
  18.     NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];  
  19.     //MyLog(@"body data %@", body);  
  20.     [request setHTTPBody:body];  
  21.     NSURLResponse *response = nil;  
  22.     NSError *error = nil;  
  23.     //第二,三个参数是指针的指针,所有要用取址符,这个方法是同步方法。同步操作没有完成,后面的代码不会执行。  
  24.     NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  
  25.       
  26.     //    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];  
  27.     //    MyLog(@"返回结果是:%@", str);  
  28.       
  29.     if (error == nil) {  //接受到数据,表示工作正常  
  30.         NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];  
  31.         MyLog(@"%@",str);  
  32.         NSRange range = [str rangeOfString:@"\"msg\":\"0\""];  
  33.         if (range.location != NSNotFound) {  
  34.             return true;  
  35.         }else{  
  36.             return false;  
  37.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错鸟"  
  38.                                                             message:@"您需要联系项目开发人员"  
  39.                                                            delegate:nil  
  40.                                                   cancelButtonTitle:@"确定"  
  41.                                                   otherButtonTitles:nil];  
  42.             [alert show];  
  43.         }  
  44.     }  
  45.       
  46.     if(error != nil || response == nil)  
  47.     {  
  48.         return false;  
  49.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误"  
  50.                                                         message:@"登陆失败,网络不稳定"  
  51.                                                        delegate:nil  
  52.                                               cancelButtonTitle:@"确定"  
  53.                                               otherButtonTitles:nil];  
  54.         [alert show];  
  55.           
  56.           
  57.     }  
  58.       
  59.     return false;  
  60. }  

0 0
原创粉丝点击