首页 > 代码库 > MKNetWorkKit使用方法
MKNetWorkKit使用方法
MKNetworkKit 是一个使用十分方便,功能又十分强大、完整的iOS网络编程代码库。它只有两个类, 它的目标是使用像AFNetworking这么简单,而功能像ASIHTTPRequest(已经停止维护)那么强大。它除了拥有 AFNetworking和ASIHTTPRequest所有功能以外,还有一些新特色,包括:
-(void)loadGet{
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"www.***.net" customHeaderFields:nil];
MKNetworkOperation *op = [engine operationWithPath:@"json/conn.asp?id=235" params:nil httpMethod:@"GET"];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
NSLog(@"%@",[completedOperation responseString]);
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
}];
[engine enqueueOperation:op];
}
POST请求:
-(void)loadPost{
MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:9502/api" customHeaderFields:nil];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:@"admin" forKey:@"username"];
[dic setValue:@"123" forKey:@"password"];
MKNetworkOperation *op = [engine operationWithPath:@"user.do" params:dic httpMethod:@"POST"];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
NSLog(@"%@",[completedOperation responseString]);
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
}];
[engine enqueueOperation:op];
}
注意:
1. HostName不需要加"http://", 程序会自动添加, 主机要指向一个目录, 不可以是一个具体文件, 如上类不可以是"www.***.net/json/conn.asp"
2. 最新版的MKNetworkKit与目前网上介绍的版本不同, 方法也发生变化,类如
[op onCompletion:^(MKNetworkOperation *completedOperation) {
<#code#>
} onError:^(NSError *error) {
<#code#>
}];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
<#code#>
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
<#code#>
}];
大家需要注意.