问题描述:

I am sending all text input strings to web by using the following encoding code I found on stackOverflow

Method 1

NSString *uniText = [NSString stringWithUTF8String:[heightValue UTF8String]];

NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];

NSString *str = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];

If I Only use Method 2

NSString *uniText = [NSString stringWithUTF8String:[heightValue UTF8String]];

After this I use URL encoding on all POST paramteres by following NSDictionary category

 NSString* postValues = [postParams serializeParams:postParams];

//Category File is separate File

#import "NSDictionary+UrlEncoding.h"

static NSString *toString(id object) {

return [NSString stringWithFormat: @"%@", object];

}

// helper function: get the url encoded string form of any object

static NSString *urlEncode(id object) {

NSString *string = toString(object);

NSString *s = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,

(CFStringRef)string,

NULL,

(CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",

kCFStringEncodingUTF8));

return s;

}

@implementation NSDictionary (UrlEncoding)

-(NSString *)serializeParams:(NSDictionary *)params {

/*

Convert an NSDictionary to a query string

*/

NSMutableArray* pairs = [NSMutableArray array];

for (NSString* key in [params keyEnumerator]) {

id value = [params objectForKey:key];

if ([value isKindOfClass:[NSDictionary class]]) {

for (NSString *subKey in value) {

NSString* escaped_value = [value objectForKey:subKey];

[pairs addObject:[NSString stringWithFormat:@"%@[%@]=%@", urlEncode(key), urlEncode(subKey), urlEncode(escaped_value)]];

}

} else if ([value isKindOfClass:[NSArray class]]) {

for (NSString *subValue in value) {

NSString* escaped_value = subValue;

[pairs addObject:[NSString stringWithFormat:@"%@[]=%@", urlEncode(key), urlEncode(escaped_value)]];

}

} else {

NSString* escaped_value = [params objectForKey:key];

[pairs addObject:[NSString stringWithFormat:@"%@=%@", urlEncode(key), urlEncode(escaped_value)]];

//[escaped_value release];

}

}

return [pairs componentsJoinedByString:@"&"];

}

@end

and this time when I receive JSON response back from server the Apple's own JSON Parser failed to parse it properly.

If I do not use the 3 Lines from Method 1. Then the json parsing completely failed and returns nil. Here is the code where I am receiving response from server

NSString *responseText = [[[[NSString alloc] initWithData:serverData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\t" withString:@""] stringByReplacingOccurrencesOfString:@"\0" withString:@""];

NSString *finalString= @"";

if (responseText) {

NSData *newdata = [responseText dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *mystring = [[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];

if (mystring) {

finalString = mystring;

}else{

finalString = responseText;

}

}

NSDictionary *dic = nil;

NSData *jsonData = [finalString dataUsingEncoding:NSUTF8StringEncoding];

NSError *error = nil;

dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];

if (error) {

NSLog(@"Error in json parsing %@",[error description]);

if (block) {

block(error,nil);

return;

}

}

I am still having trouble to parse all kinds of special characters. Like I search Paris on Map View and then The city has special character then the parsing fails. Also when I use Pound sign on keyboard and send the UTF8Encoded character the server receives \243 instead of \u00a3. While the same code Parses Euro Sign properly.

Could someone guide me in right approach to fix this issue. How do I properly send all kinds of characters on server using the iPhone. On server side the database is UTF8.

相关阅读:
Top