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表单相似的提交操作)---服务 ...
随机推荐
- rapidjson的使用
1.包含头文件 #include "rapidjson/stringbuffer.h"#include "rapidjson/document.h"#inclu ...
- java编译优化
#java编译器对`String常量表达式`的优化: - 1.String+String 可以被编译器识别为常量表达 String a="ab" ; String b=" ...
- 理解linux下源码、yum和rpm安装方法的特点
1.yum可看作在线安装,只需yum install 软件名,系统就自动根据yum源配置文件中的镜像位置去下载安装包,并可以自动分析所需的软件依赖关系,自动安装所需的依赖软件包.简单方便,不易出错,不 ...
- MySQLAdmin的用法
mysqladmin 适合于linux和windows系统 linux下:mysqladmin -u[username] -p[password] status windows下:先在安装目录找到my ...
- 相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。
相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余.
- C#编程(十一)----------C#编程规范
C#编程规范 1.要使一个代码块内的代码都同意缩进一个tab键长度 2.有下列情况下建议有换行 方法之间: 局部变量和它后边的语句之间: 方法内的功能逻辑部分之间: 3.{和}要单起一行 4.每行建议 ...
- 如何让access空值变成0?(确切的说是让access Null值变成0)
方法一 if IsNull(Me.新_退休费) = True Then Me.新_退休费 = 0 方法二 if Nz(Me.原_退休费) = Me.原_退休费 Then Me.原_退休费 = 0 有 ...
- python测试开发django-4.获取url参数和name的作用
前言 如打开博客园按时间分类标签页[https://www.cnblogs.com/yoyoketang/archive/2018/10.html],里面是时间参数是动态的,如果我想获取里面的时间参数 ...
- JDBC进阶之PreparedStatement执行SQL语句(MySQL)
一.什么是PreparedStatement 参阅Java API文档,我们可以知道,PreparedStatement是Statement的子接口(如图所示),表示预编译的 SQ ...
- FFMpeg开发使用
1.jjmpeg下载 https://code.google.com/p/jjmpeg/downloads/list 2.ffmpeg文档地址 https://www.ffmpeg.org/ 3.安卓 ...