Ran*_*ani 7 iphone objective-c
我有一个应用程序,在按钮单击我传递我的服务器api方法,它调用JSON post方法并将数据保存到服务器数据库.这里我将我的手机号码和紧急号码保存到服务器数据库.我的手机号码是字符串格式.在我的手机号码字符串变量中,我的手机号码正在保存,格式为"+ 90-9491491411".我输入+然后编码然后 - 然后编号但是当我发送到服务器数据库时我正在删除 - 并且发送no到数据库,但问题是我的服务器数据库+移动没有进入我正在输入.可能是什么问题.我正在使用POST方法发送请求.这是我的代码
-(void)sendRequest
{
NSString *newstring = txtMobile.text;
mobileValue = [newstring stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",mobileValue);
NSString *newString1 = txtemergencyprovider.text;
emergencyNumber = [newString1 stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",emergencyNumber);
if ([txtEmail.text isEqualToString:@""])
{
post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text];
NSLog(@"%@",post);
}
else {
post = [NSString stringWithFormat:@"CommandType=new&ApplicationType=%d&FullName=%@&Mobile=%@&EmergencymobileNumber=%@&Latitude=%f&Longitude=%f&City=%@&EmailAddress=%@&MobileModel=Apple",applicationtype,txtFullname.text,mobileValue,emergencyNumber,latitude,longitude,txtCity.text,txtEmail.text];
NSLog(@"%@",post);
}
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSLog(@"%@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://myapi?RequestType=NEW"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
NSLog(@"%@",webData);
}
else
{
}
}
Run Code Online (Sandbox Code Playgroud)
//在我的手机号码和紧急号码变量中,我的号码格式为"+91986444711",但是当在服务器数据库中输入值时,将被删除.可能是概率.
mtt*_*trb 17
不幸的是,NSString's -stringByAddingPercentEscapesUsingEncoding:不会将plus(+)符号转换为,%2B因为加号是一个有效的URL字符,用于分隔查询参数.这通常意味着Web服务器将其转换为空格字符.
替换加号的最简单方法是使用NSString's stringByReplacingOccurrencesOfString:withString:替换+with %2B.例如:
mobileValue = [mobileValue stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
Run Code Online (Sandbox Code Playgroud)