iphoneiosweb-servicesasihttprequestasiformdatarequest

HTTP Request with nested dictionary


I need to access a web service with a POST method, with nested dictionary to receive relevant data. But I keep getting empty response. What am I doing wrong?

    NSURL *url = [NSURL URLWithString:@"http://appzander.com/gateway.php"];

    ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];

    __weak ASIFormDataRequest *request = _request;


    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                             [NSNumber numberWithDouble:app.myLocation.longitude],@"longitude",
                             [NSNumber numberWithDouble:app.myLocation.latitude],@"latitude",
                             @"500000",@"radius",
                             @"1000",@"maxResults",
                             nil];
    NSDictionary *requestDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                                @"GetNearestStations",@"function",
                                @"false",@"debug",
                                paramsDic,@"params",
                                nil];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request appendPostData:[[SBJsonWriter new] dataWithObject:requestDic]];
    request.requestMethod = @"POST";    
    [request setDelegate:self];
    [request setCompletionBlock:^{         
        NSData *responseString = [request responseData];// responseString];
        NSLog(@"Response: %@", responseString);
        [self plotCrimePositions:responseString];
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error: %@", error.localizedDescription);
    }];

    // 6
    [request startAsynchronous];

Solution

  • Solved- I wasn't sending the parameters properly:

     NSURL *url = [NSURL URLWithString:@"http://appzander.com/gateway.php"];
    
        ASIFormDataRequest *_request = [ASIFormDataRequest requestWithURL:url];
    
        __weak ASIFormDataRequest *request = _request;
    
    
        AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    
        NSDictionary *paramsDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                                   [NSNumber numberWithDouble:32.071738],@"longitude",
                                   [NSNumber numberWithDouble:34.791295],@"langitude",
                                   @"500000",@"radius",
                                   @"1000",@"maxResults",
                                   nil];
    
        [request setPostValue:@"false" forKey:@"debug"];
        [request setPostValue:@"GetNearestStations" forKey:@"function"];
        SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
    
        NSString *paramsDicJSON = [jsonWriter stringWithObject:paramsDic];
       [request setPostValue:paramsDicJSON forKey:@"params"];
        request.requestMethod = @"POST";
        [request setDelegate:self];
        [request setCompletionBlock:^{
            NSString *responseString = [request responseString];
            [self plotBikes:responseString];
        }];
        [request setFailedBlock:^{
            NSError *error = [request error];
            NSLog(@"Error: %@", error.localizedDescription);
        }];
        [request startAsynchronous];