// 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];
// 准备AFHTTPRequestOperation
AFHTTPRequestOperation *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"]];
// 准备AFXMLRequestOperation
AFXMLRequestOperation *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];
// 流媒体数组获取
// 准备NSURLRequest
NSURLRequest *request4 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];
// 准备AFHTTPRequestOperation
AFHTTPRequestOperation *operation4 = [[AFHTTPRequestOperation alloc] initWithRequest:request4];
// 设置inputStrem
operation4.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];
// 设置outputStrem
operation4.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];
iOS开发使用AFNetworking
最新推荐文章于 2021-08-26 17:21:37 发布