1

i have this method -

NSArray *objects = [NSArray arrayWithObjects:appDelegate.UserID,itemName.text,@"1",@"1",itemDegem.text,itemDescription.text, nil];
NSArray *keys = [NSArray arrayWithObjects:@"userId",@"itemName",@"itemCategoryId",@"regionId",@"degemName",@"itemDescription", nil];

NSDictionary *registerDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonRequest = [registerDict JSONRepresentation];    
NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http://www.somedomain.com/method"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
    self.responseData = [NSMutableData data];
}

but when i send strings in my objects that not in english im getting error from the server. now, i know its a UTF8 problem, but i did set it to be UTF8. can someone please help me figure it out ?

Amir Foghel
  • 853
  • 1
  • 12
  • 28
  • 2
    What is the error you’re getting from the server? How do you know that UTF-8 is the problem? – bdesham May 07 '12 at 18:16
  • im getting some html code that basically says: The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:itemDescription. The InnerException message was 'There was an error deserializing the object of type System.String. Unexpected end of file. Following elements are not closed: itemDescription, root.'. Please see InnerException for more details.'. See server logs for more details – Amir Foghel May 07 '12 at 18:34

1 Answers1

4

I think the error is strlen([jsonRequest UTF8String]) is not equal to [jsonRequest length].

Use NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding]; to convert a NSString into NSData using UTF8 encoding.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • See: [How do I convert a NSString value to NSData?](http://stackoverflow.com/questions/901357/how-do-i-convert-a-nsstring-value-to-nsdata) – Jeffery Thomas May 07 '12 at 20:36