AFN同步异步请求

墨蓝 2022-09-25 01:20 305阅读 0赞

异步请求:

[objc] view plain copy

在CODE上查看代码片 派生到我的代码片

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

同步请求:

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

save_snippets.png

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ajax同步请求异步请求

    在前后台请求数据交互的时候,我们经常用到ajax来进行数据的请求与返回,ajax请求的async字段是boolean类型,用来标识ajax请求是同步请求或者异步请求。async