最简单的使用:
MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:@"download.finance.yahoo.com" customHeaderFields:nil];
MKNetworkOperation *op = [engine operationWithPath:@"d/quotes.csv?e=.csv&f=sl1d1t1&s=SGDUSD=X"
params:nil
httpMethod:@"GET"];
[op onCompletion:^(MKNetworkOperation *completedOperation)
{
DLog(@"Data from server %@", [completedOperation responseString]);
}onError:^(NSError* error) {
DLog("error: %@",error.description);
}];
[engine enqueueOperation:op];
NSJSONSerialization读写json例子: http://www.cnblogs.com/lovecode/articles/2305021.html
注意: MKNetworkKit的responseJSON method是使用NSJSONSerialization,所以responseJSON方法只支持iOS 5!若要连iOS 4都支持,参看 http://stackoverflow.com/questions/8974721/mknetwork-saving-json-content-to-nsdictionary
MKnetworkKit处理json format response的例子:
假设http://xxx.xxx.xxx/cap/jsontest.aspx?action=getnewslist返回的response是json format:
{"category":"Administration","newslist":[{"id":"Y14w5420q122803A526828","title":"news 1"},{"id":"43235trewa323","title":"news 2"}]}
MKNetworkEngine *engine=[[MKNetworkEngine alloc] initWithHostName:@"xxx.xxx.xxx" customHeaderFields:nil];
MKNetworkOperation *op=[engine operationWithPath:@"cap/jsontest.aspx"
params:[NSDictionary dictionaryWithObjectsAndKeys:
@"getnewslist", @"action", nil]
httpMethod:@"GET"];
[op onCompletion:^(MKNetworkOperation *operation) {
NSDictionary *response=[operation responseJSON];
NSLog([response objectForKey:@"category"]);
for(NSDictionary *newsInfo in [response objectForKey:@"newslist"])
{
NSLog([newsInfo objectForKey:@"title"]);
}
}
onError:nil
];
[engine enqueueOperation:op];