ASI和AFN实现POST异步请求的同样功能的代码
2014-11-12 22:02
609 查看
I'm a newbie in obj-c and have been using asihttp for some of my projects. When doing a post request in asihttp its done this way.
How would go about doing this is AFNetworking with a code example ?
I already got the get Json getrequest working in AFNetworking but this post request is giving me some problems. Thanks for help in advance.
答:
Updated again for AFNetworking 2.0 - see bottom
For AFNetworking 1.0:
For AFNetworking 2.0 (and also using the new NSDictionary syntax):
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; [request setPostValue:height forKey:@"user[height]"]; [request setPostValue:weight forKey:@"user[weight]"]; [request setDelegate:self]; [request startAsynchronous];
How would go about doing this is AFNetworking with a code example ?
I already got the get Json getrequest working in AFNetworking but this post request is giving me some problems. Thanks for help in advance.
答:
Updated again for AFNetworking 2.0 - see bottom
For AFNetworking 1.0:
NSURL *url = [NSURL URLWithString:@"https://mysite.com/"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: height, @"user[height]", weight, @"user[weight]", nil]; [httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"Request Successful, response '%@'", responseStr); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"[HTTPClient Error]: %@", error.localizedDescription); }];
For AFNetworking 2.0 (and also using the new NSDictionary syntax):
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *params = @{@"user[height]": height, @"user[weight]": weight}; [manager POST:@"https://mysite.com/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
相关文章推荐
- ASI和AFN实现POST异步请求的相同功能的代码
- Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
- ios网络学习------3 用非代理方法实现异步post请求
- ajax实现异步请求,不跳转页面的情况下,达到检验用户名等信息是否效果的功能!
- Android联网app必用Get请求和Post请求的区别,网络编程代码实现
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(1)----程序实现原理
- 用ASIHttpRequest通过post请求给java服务器上传的代码(不会出现乱码)
- iOS post异步请求图片上传的实现
- 【Flask】Flask实现异步非阻塞请求功能
- Ajax用法详细解析, 实现异步更新页面, 以及'post'和'get'请求属性介绍
- python构造icmp echo请求和实现网络探测器功能代码分享
- iOS--关于AFN和ASI的post登录请求
- Python 使用requests模块发送GET和POST请求的实现代码
- Java 代码实现Http 的GET和POST 请求
- 完全代码实现UITableView下拉更新,异步请求数据
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- 通过异步请求实现报表组功能http://www.runqian.com.cn/archives/5059.html/
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(3)----BufferedReader与BufferedWriter的使用
- 使用Httpclient向百度翻译提交POST请求 接受返回的JSON字符串 实现翻译功能(2)----Httpclient使用详解
- 原生JavaScript实现ajax异步请求代码