AFNetworking
AFNetworking是一种网络请求第三方封装的类库.
1 2 3 4
| _manager = [AFHTTPSessionManager manager]; _manager.responseSerializer = [AFJSONResponseSerializer serializer]; NSString * ss = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//将汉字进行一下编码 _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
|
GET请求
1 2 3 4 5 6 7 8 9
| AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager manager]; //2.0bug 必须要设置text/html 否则报1016错误 manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"]; [manager GET:@"http://1000phone.net:8088/app/openfire/api/user/near.php?latitude=40.02212&longitude=116.4343&radius=100" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
|
POST请求
1 2 3 4 5 6 7 8 9 10 11 12
| AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"]; NSDictionary*dic=@{@"key":@"value"}; [manager POST:@"http://1000phone.net:8088/app/openfire/api/user/near.php?latitude=40.02212&longitude=116.4343&radius=100" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }];
|
表单式上传文件
1 2 3 4 5 6 7 8
| [manager POST:@"" parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSURL*file= [NSURL fileURLWithPath:@"图片地址"]; [formData appendPartWithFileURL:file name:@"image" error:nil]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
|
如何使用AFNetworking下载一个文件?
先创建一个AFURLConnectionOperation对象,然后再使用它的属性outputStream进行处理
1
| operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];
|
当应用程序退出时,如何保持持续的请求?
AFURLConnectionOperation有一个叫setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于处理在应用程序进入后台后,进行持续的请求
1 2
| [self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ }];
|
问题
AFNetWorking的JSON解析默认库是使用的AFJSONRequestOperation模式,只支持text/json,application/json,text/javascript,所以如果出现code=-1016错误则说明当前的JSON解析模式是text/html,所以要加上这段代码
1 2
| AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];
|
XML 请求
1 2 3 4 5 6 7 8 9 10
| NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792@N01&format=rest"]]; AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) { XMLParser.delegate = self; [XMLParser parse]; } failure:nil]; [operation start];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];
|
图片上传处理,监测上传状态:
1 2 3 4 5 6 7 8 9 10 11
| NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5); NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation start];
|
在线流媒体请求
1 2 3 4 5
| NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]]; operation.outputStream = [NSOutputStream outputStreamToMemory]; [operation start];
|