-(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#>
}];
大家需要注意.