1

我正在使用 MKNewtworkkit 进行网络操作是我的 ios 应用程序。我在服务器中有 JSONInterface,我可以通过它检索 JSONArray。我也能够在 android 应用程序和浏览器中获取数据,但是当我尝试使用 MKNetworkkit [completedOperation responseJSON] 获取 ftech 数据时返回 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];
4

1 回答 1

2

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

看起来您正在将“responseJSON”转换为 NSArray,但它应该是 NSDictionary。

这对我有用->

    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];
于 2015-01-12T19:32:54.847 回答