7

我有一个应用程序,单击按钮我传递我的服务器 api 方法,该方法调用 JSON post 方法并将数据保存到服务器数据库。这里我将我的手机号码和紧急号码保存到服务器数据库。我的手机号码是字符串格式.在我的手机号码字符串变量中,我的手机号码正在保存,格式为“+90-9491491411”。我输入+,然后输入代码,然后输入数字,但是当我发送到服务器数据库时,我正在删除- 并将否发送到数据库,但问题出在我的服务器数据库中 + 没有输入我正在输入的移动设备。可能是什么问题。我正在使用 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 
    {

    }

}

//在我的手机号码和紧急号码变量中,我的号码是这种格式'+91986444711',但是当在服务器数据库中输入值时+被删除。可能是什么问题。

4

3 回答 3

17

不幸的是,NSString's-stringByAddingPercentEscapesUsingEncoding:不会将加号 ( +)转换为%2B,因为加号是用于分隔查询参数的有效 URL 字符。这通常意味着它被 Web 服务器转换为空格字符。

替换加号的最简单方法是使用NSString'sstringByReplacingOccurrencesOfString:withString:替换+with %2B。例如:

mobileValue = [mobileValue stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
于 2012-04-10T09:38:31.830 回答
1

URL 中的加号(“+”)表示编码的空格(“”),您的服务器很可能会将其解释为空格。在发布之前将字符串中的加号字符更改为 %2B。有关 URL 编码的完整解决方案,请参阅这篇文章: http: //madebymany.com/blog/url-encoding-an-nsstring-on-ios

于 2012-04-10T08:52:30.443 回答
0

加号表示 post 请求中的空格。您需要将加号转换为百分比转义字符。最简单的方法如下:

NSString* escapedMobileValue = [mobileValue stringByReplacingOccurencesOfString: @"+" withString: @"%2b"];

这将+变成%2b. 可能服务器会自动为您反转编码。

(根据 mttrb 的评论编辑)

于 2012-04-10T09:23:25.360 回答