AFN同步异步请求
异步请求:
-(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同步异步请求的更多相关文章
- Springmvc中 同步/异步请求参数的传递以及数据的返回
转载:http://blog.csdn.net/qh_java/article/details/44802287 注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方 ...
- libcurl的封装,支持同步异步请求,支持多线程下载,支持https
最近在做一个项目,需要用到http get post等 需求分析需要做到同步和异步,异步请求的返回以可选的回调通知的方式进行. 本人以Linux为例,一步一步的来实现. 配置并且编译libcurl我以 ...
- 从零开始学 Web 之 Ajax(五)同步异步请求,数据格式
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- http 同步异步请求
在用户交互模式下,当你改变表单中某个组件的值时, 譬如你填写名字.修改性别.选择爱好的时候,浏览器和服 务器至今没有发生任何交互,只有当你点击submit的时候, 浏览器才会把你的参数,也就是form ...
- springmvc中同步/异步请求参数的传递以及数据的返回
注意: 这里的返回就是返回到jsp页面 **** controller接收前台数据的方式,以及将处理后的model 传向前台***** 1.前台传递数据的接受:传的属性名和javabean的属性相同 ...
- okhttp框架源码分析从同步&异步请求使用开始
对于okhttp在如今项目中的普及程度已经不言而喻啦,基本上如今网络请求都会基于它去进行封装,而非前几年用Android的网络框架HttpURLConnection和Apache HttpClient ...
- jQuery基础(Ajax,load(),getJSON(),getScript(),post(),ajax(),同步/异步请求数据)
1.使用load()方法异步请求数据 使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为: load(url,[data],[callba ...
- AJAX请求详解 同步异步 GET和POST
AJAX请求详解 同步异步 GET和POST 上一篇博文(http://www.cnblogs.com/mengdd/p/4191941.html)介绍了AJAX的概念和基本使用,附有一个小例子,下面 ...
- 普通B/S架构模式同步请求与AJAX异步请求区别(个人理解)
在上次面试的时候有被问到过AJAX同步与异步之间的概念问题,之前没有涉及到异步与同步的知识,所以特意脑补了一下,不是很全面... 同步请求流程:提交请求(POST/GET表单相似的提交操作)---服务 ...
随机推荐
- hdu1238 Substrings (暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit : 2000/1000ms (Java/Other) Me ...
- Bootstrap 3之美01-下载并引入页面
本篇主要包括: ■ 下载Bootstrap 3■ Bootstrap 3引入页面 下载Bootstrap 3 →打开网站:http://getbootstrap.com/→点击屏幕中央位置的Down ...
- python文本 字符串逐字符反转以及逐单词反转
python文本 字符串逐字符反转以及逐单词反转 场景: 字符串逐字符反转以及逐单词反转 首先来看字符串逐字符反转,由于python提供了非常有用的切片,所以只需要一句就可以搞定了 >>& ...
- 【docker-compose】使用docker-compose启动spring-boot+mysql,服务内连数据库报错:create connection SQLException, url: jdbc:mysql://mysql:33061/swapping?useSSL=false&useUnicode=true&characterEncoding=UTF-8, errorCode 0,
完整配置在这里, https://www.cnblogs.com/sxdcgaq8080/p/10070948.html 启动报错如下: 2018-12-05 01:04:05.569 ERROR 1 ...
- EntityFramework(EF)贪婪加载和延迟加载的选择和使用
贪婪加载:顾名思议就是把所有要加载的东西一 次性读取 1 using (var context = new MyDbContext()) 2 { 3 var orders = from o in co ...
- C#中的托管与非托管
在.net 编程环境中,系统的资源分为托管资源和非托管资源. 字面理解托管,就是托付个别人管理,要的是结果,具体怎么完成的我并不关心,就像某些'牛逼'的老板“我只要结果”那样. 在.NET FRAME ...
- applicationContext.xml中的使用${}是代表什么意思?
在applicationContext.xml文件中,使用 ${xxx} 表示的是:调用某个变量,该变量的名称就是{xxx}里面的"xxx". 例如:在applicationC ...
- 局域网Cesium离线影像及瓦片影像地图加载【转】
http://www.mamicode.com/info-detail-2161992.html 1.Cesium简介 优点: cesium展示地图数据效果比较好,解析2D地图各种不同服务类型的数据源 ...
- 关于一道JS面试题的思考
题目: ; i < ; i++) { setTimeout(function() { console.log(new Date, i); }, ); } console.log(new Date ...
- C++11中万能的可调用类型声明std::function<...>
在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...