iOS开发使用AFNetworking
来源:互联网 发布:boost linux 编辑:程序博客网 时间:2024/04/28 00:01
// 1. 需要加入SystemConfiguration.framework、MobileCoreServices.framework、Security.framework三个框架// 2. 消除警告在pch文件中添加 #import #import// 3. 引入AFNetworking框架 #import "AFNetworking.h"// 4. 下面是详细代码,包括:简单网络请求、图片请求、JSON数据请求、XML数据请求、流媒体播放功能// 普通网络请求NSURL *url = [NSURL URLWithString:@"http://192.168.198.125/admin/index.php?m=interface&a=companies"];NSURLRequest *request = [NSURLRequest requestWithURL:url];// 准备AFHTTPRequestOperationAFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSArray *array = operation.responseString; NSLog(@"%@", array); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure: %@", [error localizedDescription]);}];// 开始获取数据[operation start];// 图片网络请求UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];[self.view addSubview:image];// 使用方法获取图片和密码[image setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"Default.png"]];// JSON数据网络请求NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.198.125/admin/index.php?m=interface&a=companies"]];// 准备AFJSONRequestOperation对象AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { // 请求成功 NSLog(@"%@", operation.responseString);} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { // 请求失败}];// 发送网络请求[operation start];// XML数据网络请求NSURLRequest *request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]];// 准备AFXMLRequestOperationAFXMLRequestOperation *operation2 = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request2 success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { // 获取成功 XMLParser.delegate = self; [XMLParser parse];} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) { // 获取失败}];// 开始获取数据[operation2 start];// 流媒体数组获取// 准备NSURLRequestNSURLRequest *request4 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];// 准备AFHTTPRequestOperationAFHTTPRequestOperation *operation4 = [[AFHTTPRequestOperation alloc] initWithRequest:request4];// 设置inputStremoperation4.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];// 设置outputStremoperation4.outputStream = [NSOutputStream outputStreamToMemory];[operation4 start];// 添加上传图片代码 // 将图片转为NSData形式NSData *imageData = UIImagePNGRepresentation(_thumbnailView.image);// 要上传的地址NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.198.125/index.php?m=Interface&a=aptitude"]];// 初始化AFHttpClient对象AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];// 初始化NSURLRequest对象NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" // 发送请求的方式,需设置为POST方式 path:Nil // 路径,可为空 parameters:@{ @"tel" : @"13900000000", @"tenantid" : kServerPort, @"aptitude" : _aptitudeNameText.text } // 参数设置 constructingBodyWithBlock:^(id<<span style="color: #5a8487">AFMultipartFormData> formData) { // 设置上图片的数据 [formData appendPartWithFileData:imageData name:@"aptitude_logo" // 图片参数的名称 fileName:@"aptitude_logo.png" // 上传的图片名称,可与参数名称不一致 mimeType:@"image/png"]; // 图片类型 }];// 初始化AFHTTPRequestOperation对象AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];// 处理上传数据的结果的代码块[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { // 上传成功,接收返回来的结果 NSLog(@"%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 上传失败,打印错误信息 NSLog(@"错误信息: %@", [error localizedDescription]); }];// 开始上传 [client.operationQueue addOperation:operation];
0 0
- iOS开发使用AFNetworking
- ios开发点滴-AFNetworking 使用
- iOS开发之AFNetworking使用
- iOS开发之AFNetworking使用
- iOS开发AFNetworking的使用
- iOS开发AFNetworking的使用
- iOS开发网络数据之AFNetworking使用
- IOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- iOS开发网络数据之AFNetworking使用
- 【iOS】开发网络数据之AFNetworking使用
- Linux系统下UDP发送和接收广播消息小例子
- ORACLE ORA-02030: can only select from fixed tables/views
- Dojo与jQuery综合比较分析
- 页游大佬烧端游冷灶 不是说都没落了吗?
- 手机密码设置后经济发展610年
- iOS开发使用AFNetworking
- Java Map遍历方式的选择
- Server_网页如何启用Gzip压缩以提高浏览速度?
- http 超时设置
- KBEngine引擎使用笔记
- linux下 导入导出数据库
- Image inpainting
- juqery ajax与php返回json数据
- 1501081204-hpu-1023: 【C语言训练】斐波纳契数列