我正在尝试调用网络服务。我试过了
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.75.1:8082/projectname/public/tests"]];
NSURLSessionConfiguration *configuration = [ NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *localFile, NSURLResponse *response, NSError *error) {
if(!error){
NSLog(@"no error");
}else{
NSLog(@"error");
}
}];
[task resume];
}
如您所见,有两个 nslog
语句。我得到了 no error
一个。
当我从我的 safari 调用该 Web 服务时,我在浏览器中打印了一个简单的 index
字符串,请问我如何在我的 xcode 中看到该字符串?
谢谢
最佳答案
你可以实现委托(delegate)方法
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location;
编辑
试试这个
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:YOUR URL]];
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"~~~~~ Status code: %d", [response statusCode]);
//Print your recived data here..
NSString *str = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);
关于IOS 7如何打印url响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24824633/