sommeralex wrote:
My String I want to pass (with json) is
{"lat":"48","long":"16","text":"!@&"}
the generated text is
&message=%7B%22lat%22:%2248%22,%22long%22:%2216%22,%22text%22:%22%3C%3E!@&%22%7D
the method I am using:
NSString *codeMessage = [message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
tempString = [tempString stringByAppendingFormat:@"&message=%@", codeMessage];
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
It fairly clear that stringByAddingPercentEscapesUsingEncoding is
NOT converting the "&" near the end of your initial string. That means the final result looks like &message=xxx&yyy which is probably not what you want.
How is the original string created, and do the special characters at the end change, or mean something as far as an action that should be taken?
Obviously this is some kind of location, followed by a text field.
If the characters other than the lat/long ("!@&") are static, then I would encode them once, by hand to insure the & is encoded. Another option, if you do not know whether the & is going to appear in the text, is to do:
NSString *codeMessage = [message stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
codeMessage = [codeMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
tempString = [tempString stringByAppendingFormat:@"&message=%@", codeMessage];
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
Obviously the first string substitution needs to be done BEFORE you add the text to "&message=xxx". It's understandable that "&" is not substituted, since you could have "&message=foo&text=bar&etc=more". Maybe Ray or someone else knows if there is a better method to call to change the special characters to hex characters.
However, if this is the only character not getting converted, the extra replacement should solve the problem.