在 ios 应用程序 (objective-c) 的服务器上发布数据
Posting data on server in ios app (objective-c)
这是我在服务器上 post 地址所使用的方法。
但是当单击按钮时,它会转到 stopActivityIndicatorInView 。
但没有 posting 数据,它只显示 UIalertview。我做错的地方需要一些帮助。
当我将参数更改为 "user_id" 时出现异常。
-(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];
}];
}
这些是我从服务器得到的参数。
$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'] : '';
如果它显示警报视图,则表示请求不成功,请仔细检查您的参数,例如用户 ID,您漏掉了下划线,因此它应该是这样的:"user_id" 而不是共 "userid"
这是我在服务器上 post 地址所使用的方法。 但是当单击按钮时,它会转到 stopActivityIndicatorInView 。
但没有 posting 数据,它只显示 UIalertview。我做错的地方需要一些帮助。
当我将参数更改为 "user_id" 时出现异常。
-(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];
}];
}
这些是我从服务器得到的参数。
$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'] : '';
如果它显示警报视图,则表示请求不成功,请仔细检查您的参数,例如用户 ID,您漏掉了下划线,因此它应该是这样的:"user_id" 而不是共 "userid"