Search code examples
iosobjective-cafnetworking

Posting data on server in ios app (objective-c)


This is the method I am using to post address on the server. But when click the button it goes to stopActivityIndicatorInView .

But not posting the data and it only shows UIalertview. I need some help where I am doing wrong.

when I change parameters to "user_id" it goes to the exception.

-(void)ServiceAddress
{

    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    [Utils startActivityIndicatorInView:self.view withMessage:@"Please wait...."];
    NSString *strurl = [NSString stringWithFormat:@"URL"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"] forKey:@"userid"];
    [parameters setObject:_txtcity.text forKey:@"city"];
    [parameters setObject:_txthome.text forKey:@"house_no"];
    [parameters setObject:_txtflat.text forKey:@"locality"];
    [parameters setObject:_txtpincode.text forKey:@"pin_code"];
    [parameters setObject:_txtCurrentLocation.text forKey:@"address"];
    [parameters setObject:_txtmobile.text forKey:@"mobile"];

    [manager POST:strurl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary*    data1 = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"JSON: %@", data1);
        if([[[data1 objectForKey:@"commandResult"] objectForKey:@"success"]integerValue]==1)
        {
            [Utils stopActivityIndicatorInView:self.view];

            UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            MyAddressVC *view=[story instantiateViewControllerWithIdentifier:@"MyAddressVC"];
            [self.navigationController pushViewController:view animated:NO];

        }
        else
        {

            [Utils stopActivityIndicatorInView:self.view];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"go back"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", [error localizedDescription]);
        [Utils stopActivityIndicatorInView:self.view];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }];
}

these are the parameters I got from server.

$userId = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
$mobile = isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : '';
$houseNo = isset($_REQUEST['house_no']) ? $_REQUEST['house_no'] : '';
$street = isset($_REQUEST['street']) ? $_REQUEST['street'] : '';
$locality = isset($_REQUEST['locality']) ? $_REQUEST['locality'] : '';
$city = isset($_REQUEST['city']) ? $_REQUEST['city'] : '';
$state = isset($_REQUEST['state']) ? $_REQUEST['state'] : '';
$pinCode = isset($_REQUEST['pin_code']) ? $_REQUEST['pin_code'] : '';
$latitude = isset($_REQUEST['latitude']) ? $_REQUEST['latitude'] : '';
$longitude = isset($_REQUEST['longitude']) ? $_REQUEST['longitude'] : '';

Solution

  • If it's displaying the alert view, it means the request wasn't successful, double check your parameters, for example for user id, you have missed the underscore so it should be like this: "user_id" instead of "userid"