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表单相似的提交操作)---服务 ...
随机推荐
- ROS知识(18)----Pluginlib原理
目录 Overview Example Providing a Plugin Registering/Exporting a Plugin The Plugin Description File Re ...
- newtonsoft动态修改JObject
直接上代码: var data = new { mobile = ", company = ", isTravel = ", invoiceId = ", se ...
- C#程序集系列05,让程序集包含多个module
本篇体验在一个程序集中包含多个module. □ 创建3个module →删除F盘as文件夹中的一些文件,只剩下如下3个文件→用记事本打开MyFirstModule.cs文件,修改如下,并保存 usi ...
- 用最简单的例子理解复合模式(Composite Pattern)
在显示树形结构时,复合模式有很好的体现.本篇显示如下部门结构: 以上,有的节点包含子节点,有的节点部包含子节点.不管是什么节点,每个节点就代表一个部门. 首先设计一个关于部门的抽象基类. public ...
- spring mvc 下 applicationContext 和webApplicationContext
spring中的ApplicationContexts可以被限制在不同的作用域.在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了Di ...
- 借助LVS+Keepalived实现负载均衡
原文地址:http://www.cnblogs.com/edisonchou/p/4281978.html 一.负载均衡:必不可少的基础手段 1.1 找更多的牛来拉车吧 当前大多数的互联网系统都使用了 ...
- Asp.Net Core App 部署故障示例 1
相关阅读:Windows + IIS 环境部署Asp.Net Core App 1. HTTP Error 502.5 – Process Failure 环境 Windows Server 201 ...
- OpenCV教程(42) xml/yaml文件的读写
参考资料: http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html #include "opencv2/openc ...
- 读书笔记-C#中装箱拆箱性能
前言 最近在看王涛大神的<你必须知道的.NET(第二版)>一书,嗯,首先膜拜一下…. 在书中的第五章-品味类型中,对装箱与拆箱一节感触很深,概念本身相信每一个程序猿都不陌生,装 ...
- Bootstrap popover弹出框
popover被挤压.遮挡的问题: 弹出框显示的时候如果贴近一个列的边沿,就会很窄或被遮挡,解决起来很简单,只需在初始化的时候添加一个container属性就可以了: $(function (){ $ ...