MKNetworkkit:从服务器获取JSONArray

时间:2012-11-30 19:18:43

标签: ios5 mknetworkkit mknetworkengine

我正在使用MKNewtworkkit进行网络操作是我的ios应用程序。我在服务器中有JSONInterface,通过它我可以检索JSONArray。我也可以在Android应用程序和浏览器中获取数据,但是当我尝试使用MKNetworkkit fFT数据时[completedOperation responseJSON]返回nil。你能帮我吗

MKNetworkEngine *network_engine= [[MKNetworkEngine alloc] initWithHostName:@"****.com"];
 NSMutableDictionary* params=[[NSMutableDictionary alloc] initWithCapacity:3];
[params setObject:[self.userName text] forKey:@"user"];
[params setObject:[self.password text] forKey:@"password"];
[params setObject:@" " forKey:@"meters"];
MKNetworkOperation* operation =[network_engine operationWithPath:@"q/out" params:params httpMethod:@"GET" ssl:NO]; 
[operation onCompletion:^(MKNetworkOperation *completedOperation) {
    NSLog(@"response: %@",[completedOperation responseString]);
    @try {
        NSArray *data_array=[completedOperation responseJSON];
        for (int i=0; i<[data_array count]; i++) {
            NSDictionary *dict=[data_array objectAtIndex:i];
            NSString *id=[dict valueForKey:@"mid"];
            NSString *location=[dict valueForKey:@"location"];
            NSLog(@"id:%@ , location:%@",id,location);
        }
    }

    @catch (NSException *exception) {
        NSLog(@"Exception: %@",[exception name]);
    }

} onError:^(NSError *error) {
         NSLog(@"Couldn't make network call");
}];
[network_engine enqueueOperation:operation];

1 个答案:

答案 0 :(得分:2)

我知道这是一个老帖子,但万一其他人遇到这个......

看起来您正在将'responseJSON'作为NSArray进行投射,但它应该是NSDictionary。

这对我有用 - &gt;

    MKNetworkOperation *op = [mkNetworkEngine operationWithURLString:CONFIG_FILE_URL];
[op setShouldNotCacheResponse:YES];

[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {

    NSLog(@"** Success downloading config file...");

    NSDictionary* responseJSON = completedOperation.responseJSON;


} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {

    NSLog(@"** Failed downloading config file - error: %@",error);

}];

[mkNetworkEngine enqueueOperation:op];