1

I am using MKNewtworkkit for networking operation is my ios app. I have JSONInterface in server through which i can retrieve JSONArray. I am able to fetch data in android app and browser as well, But when i try to ftech data using MKNetworkkit [completedOperation responseJSON] is returning nil. Could you please help me

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];
Venu Gandhe
  • 77
  • 1
  • 8

1 Answers1

2

I know this is an old post but in case anyone else comes across this...

It looks like you are casting the 'responseJSON' as an NSArray but it should be a NSDictionary.

This works for me ->

    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];
Ian Newland
  • 1,574
  • 1
  • 18
  • 20