Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

iphone - Printing NSData using NSLog

How can I print the contents of an NSData object using NSLog:

-(void) post:(NSString*) msg to:(NSString*) link{
    NSString *myRequestString = [NSString stringWithFormat:@"message=%@", msg];
    NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: link]]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: myRequestData];
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
    NSLog("%@", *returnData); //doesn't work

}

I would like to print the contents of *returnData...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert NSData to NSString using

NSString *strData = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];

and print NSString in NSLog like below

NSLog(@"%@",strData);

This answer is edited for JeremyP, as he does not know how to know content is of UTF-8, though it was not a discussion of this question.

You can get response header in following delegate method

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *dic = [httpResponse allHeaderFields];
}

This dictionary will give you entire header info like below

<CFBasicHash 0x5a45e40 [0x24b2380]>{type = immutable dict, count = 7,
entries =>
0 : <CFString 0x5d1bf60 [0x24b2380]>{contents = "X-Aspnet-Version"} = <CFString 0x5d21a60 [0x24b2380]>{contents = "2.0.50727"}
1 : <CFString 0x41a03a8 [0x24b2380]>{contents = "Server"} = <CFString 0x5d272f0 [0x24b2380]>{contents = "Microsoft-IIS/6.0"}
2 : <CFString 0x41a0010 [0x24b2380]>{contents = "Content-Length"} = <CFString 0x5d28630 [0x24b2380]>{contents = "385"}
6 : <CFString 0x419ff48 [0x24b2380]>{contents = "Cache-Control"} = <CFString 0x5d29c70 [0x24b2380]>{contents = "private, max-age=0"}
10 : <CFString 0x5d1c640 [0x24b2380]>{contents = "X-Powered-By"} = <CFString 0x5d26f10 [0x24b2380]>{contents = "ASP.NET"}
11 : <CFString 0x41a0060 [0x24b2380]>{contents = "Content-Type"} = <CFString 0x5d29c90 [0x24b2380]>{contents = "text/xml; charset=utf-8"}
12 : <CFString 0x41a0088 [0x24b2380]>{contents = "Date"} = <CFString 0x5d27610 [0x24b2380]>{contents = "Fri, 08 Jul 2011 15:23:10 GMT"}
} 

Check charset="utf-8", you will get encoding from here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...