objective c - A string that eats up whatever is in front? -
i know weird question have string inside rkresponse (from restkit) eating whatever add after it.
i want know inside "response.bodyasstring" causes behavior.
this code:
nslog(@"--- refreshfriendslist callback ---"); nslog(@"status code: %i",response.statuscode); nslog(@"response contents: 54321%@12345",response.bodyasstring); nslog(@"response contents length: %@",response.contentlength); if ([response isok]) { nslog(@"response ok"); nslog(@"length %lu",(unsigned long)response.bodyasstring.length); nslog(@"trimmed lenght %lu",(unsigned long)[response.bodyasstring stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]].length); nslog(@"is empty? %@",[response.bodyasstring isequaltostring:@""]?@"yes":@"no"); . . .
and output:
2013-09-04 15:26:31.958 appname[5658:707] --- refreshfriendslist callback --- 2013-09-04 15:26:31.965 appname[5658:707] status code: 200 2013-09-04 15:26:31.980 appname[5658:707] response contents: 54321 2013-09-04 15:26:31.986 appname[5658:707] response contents length: 4 2013-09-04 15:26:31.990 appname[5658:707] response ok 2013-09-04 15:26:31.997 appname[5658:707] length 4 2013-09-04 15:26:32.002 appname[5658:707] trimmed lenght 4 2013-09-04 15:26:32.014 appname[5658:707] empty? no
as can see later 12345 disappeared , have no idea why, neither why length of string 4.
the server should returning empty string created having found no results
jsonobject friendsandfamilyinfo = manager.getfriendsandfamilylist(userid); if (friendsandfamilyinfo != null) { resp.getwriter().print(friendsandfamilyinfo.tostring()); resp.setstatus(200); } else { resp.getwriter().print("error"); resp.setstatus(500); }
update:
this when print response raw data:
2013-09-04 15:47:59.930 appname[5701:707] response data: <007b007d>
and how restkit coverts data string:
- (nsstring *)bodyasstring { return [[[nsstring alloc] initwithdata:self.body encoding:[self bodyencoding]] autorelease]; }
this looks encoding problem: output
appname[5701:707] response data: <007b007d>
one can see returned data string {}
in utf-16 (big-endian) encoding. if converted nsstring
using e.g. utf-8 or ascii encoding first character of converted string nul character.
then in
nslog(@"response contents: 54321%@12345",response.bodyasstring);
the nul character acts string terminator.
it explain why length of string 4: each byte of data converted 1 unicode.
example:
uint8_t bytes[] = { 0x00, 0x7b, 0x00, 0x7d }; nsdata *data = [nsdata datawithbytes:bytes length:sizeof(bytes)]; nslog(@"%@", data); // output: <007b007d> nsstring *str = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; nslog(@"contents: >>>%@<<<",str); // output: contents: >>> nslog(@"length: %d",[str length]); // output: 4
but if correct encoding used:
nsstring *str = [[nsstring alloc] initwithdata:data encoding:nsutf16bigendianstringencoding]; nslog(@"contents: >>>%@<<<",str); // output: contents: >>>{}<<< nslog(@"length: %d",[str length]); // output: 2
Comments
Post a Comment