iOS开发之AFNetworking

本文介绍了一个使用AFNetworking的示例项目,包括通过FKBooksController和FKAuthorsController两个类实现从服务器获取图书和作者信息的过程。文章展示了如何创建UITableViewController子类以加载和展示数据,并通过AFHTTPRequestOperationManager发起HTTP请求。

下面是一个AFNetworking使用的小例子:

1.创建FKBooksController类 : UITableViewController

#import <UIKit/UIKit.h>

@interface FKBooksController : UITableViewController
@property (nonatomic, strong) NSDictionary* selectedAuthor;
@end
#import "FKBooksController.h"
#import "FKAppDelegate.h"

@interface FKBooksController ()
{
	NSArray* books;
	FKAppDelegate* appDelegate;
}
@end
@implementation FKBooksController
- (void)viewDidLoad
{
    [super viewDidLoad];
	appDelegate = [UIApplication sharedApplication].delegate;	
	NSString* url = @"http://192.168.1.88:8888/AFNetworkingServer/books.json";
	self.navigationItem.title = [NSString stringWithFormat:@"%@的图书"
		, [self.selectedAuthor objectForKey:@"name"]];
	// 使用NSDictionary封装请求参数
	NSDictionary *parameters = @{@"authorId":
		[self.selectedAuthor objectForKey:@"id"]};
	// 使用AFHTTPRequestOperationManager发送GET请求
	[appDelegate.manager POST:url parameters:parameters
		// 获取服务器响应成功时激发的代码块
		success:^(AFHTTPRequestOperation *operation, id responseObject)
		{
			// 将服务器响应的JSON数据转换为Objective-C对象,赋值给books属性
			books = responseObject;
			// 重新加载表格数据
			[self.tableView reloadData];
	 	}
		// 获取服务器响应失败时激发的代码块
		failure:^(AFHTTPRequestOperation *operation, NSError *error)
		{
			NSLog(@"获取图书信息出现错误: %@", error);
		}];
}
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
	return books.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	// 根据动态单元格原型的ID来获取可重用单元格
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
							 @"bookCell" forIndexPath:indexPath];
	// 获取当前行号在books中对应的数据
	NSDictionary* book = [books objectAtIndex:indexPath.row];
	// 获取单元格中3个控件,并为3个控件设置显示文本
	UILabel* titleLable = (UILabel*)[cell viewWithTag:1];
	titleLable.text = [book objectForKey:@"title"];
	UILabel* authorLable = (UILabel*)[cell viewWithTag:2];
	authorLable.text = [book objectForKey:@"author"];
	UILabel* remarkLabel = (UILabel*)[cell viewWithTag:3];
	remarkLabel.text = [book objectForKey:@"remark"];
	return cell;
}
@end

2.创建FKAuthorsController类 : UITableViewController,导入FKBooksController和FKAppDelegate

#import "FKAuthorsController.h"
#import "FKBooksController.h"
#import "FKAppDelegate.h"

@interface FKAuthorsController ()
{
	NSArray* authors;
	FKAppDelegate* appDelegate;
}
@end
@implementation FKAuthorsController
- (void)viewDidLoad
{
	[super viewDidLoad];
	appDelegate = [UIApplication sharedApplication].delegate;
	NSString* url = @"http://192.168.1.88:8888/AFNetworkingServer/authors.json";
	// 使用AFHTTPRequestOperationManager发送GET请求
	[appDelegate.manager GET:url parameters:nil
		// 获取服务器响应成功时激发的代码块
		success:^(AFHTTPRequestOperation *operation, id responseObject)
		{
			// 将服务器响应的JSON数据转换为Objective-C对象,赋值给authors属性
			authors = responseObject;
			// 重新加载表格数据
			[self.tableView reloadData];
		}
		// 获取服务器响应失败时激发的代码块
		failure:^(AFHTTPRequestOperation *operation, NSError *error)
		{
			NSLog(@"获取作者信息出现错误: %@", error);
		}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
	UITableViewCell* cell = (UITableViewCell*)sender;
	// 获取激发跳转的单元格所在的NSIndexPath
	NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
	// 获取即将跳转到的目标视图控制器
	FKBooksController* booksController = (FKBooksController*)
		segue.destinationViewController;
	// 将用户选中的单元格的作者信息传给目标视图控制器
	booksController.selectedAuthor = [authors objectAtIndex:indexPath.row];
}
// --------下面两个方法根据authors集合的元素来显示表格数据---------
- (NSInteger)tableView:(UITableView *)tableView
 	numberOfRowsInSection:(NSInteger)section
{
	return authors.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
	cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
		@"authorCell" forIndexPath:indexPath];
	NSDictionary* dict = [authors objectAtIndex:indexPath.row];
	cell.textLabel.text = [dict objectForKey:@"name"];
	cell.detailTextLabel.text = [dict objectForKey:@"desc"];
	return cell;
}
@end

转载于:https://my.oschina.net/khakiloveyty/blog/397028

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值