php/json 服务中的 NSURLRequest(异步)未获取任何数据
Get no data from NSURLRequest (asynchronous) from php/json service
我在从网站检索数据到 iOS 设备时遇到问题。网络使用 php 文件从 mySQL 数据库中读取一些数据。然后它以 json 格式发回数据。
我正在使用以下代码接收数据。代码放入viewDidLoad。为了避免 UI 相关代码在不等待网络响应的情况下执行,我将 NSURLRequest/NSURLConnection 操作放入带有 GCD 的异步队列中。仍然,控制台 window 显示 self.connectionData 没有数据的 NSLog。
这个结果和我直接把NSURLRequest/NSURLConnection操作放到viewDidLoad函数中是一样的。你知道这里出了什么问题吗?为什么我无法获取数据?
当我直接从浏览器 运行 php 时,Web 服务 运行 没问题。
非常感谢!
此致,
保罗
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
dispatch_sync(concurrentQueue, ^{
NSString *urlAsString = @"http://www.myexamplesite.com/loadProgram.php";
NSURL *url=[NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0F];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue: queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if ([data length]>0 && connectionError == nil) {
[self.connectionData appendData:data];
NSLog(@"datalength is %ld", [self.connectionData length]);
NSString *retString = [[NSString alloc] initWithData:self.connectionData encoding:NSUTF8StringEncoding];
NSLog(@"json returned: %@ <end>", retString);
}
else if ([data length]==0 && connectionError ==nil)
{NSLog(@"Nothing was downloaded!");
}
else if (connectionError != nil){
NSLog(@"Error happend = %@", connectionError);
}
}];
});
});
}
您没有初始化self.connectionData
。因此,您需要在开头添加以下内容:
if (!self.connectionData) {
self.connectionData = [[NSMutableData alloc] init];
}
我在从网站检索数据到 iOS 设备时遇到问题。网络使用 php 文件从 mySQL 数据库中读取一些数据。然后它以 json 格式发回数据。
我正在使用以下代码接收数据。代码放入viewDidLoad。为了避免 UI 相关代码在不等待网络响应的情况下执行,我将 NSURLRequest/NSURLConnection 操作放入带有 GCD 的异步队列中。仍然,控制台 window 显示 self.connectionData 没有数据的 NSLog。
这个结果和我直接把NSURLRequest/NSURLConnection操作放到viewDidLoad函数中是一样的。你知道这里出了什么问题吗?为什么我无法获取数据?
当我直接从浏览器 运行 php 时,Web 服务 运行 没问题。
非常感谢!
此致, 保罗
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
dispatch_sync(concurrentQueue, ^{
NSString *urlAsString = @"http://www.myexamplesite.com/loadProgram.php";
NSURL *url=[NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0F];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue: queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if ([data length]>0 && connectionError == nil) {
[self.connectionData appendData:data];
NSLog(@"datalength is %ld", [self.connectionData length]);
NSString *retString = [[NSString alloc] initWithData:self.connectionData encoding:NSUTF8StringEncoding];
NSLog(@"json returned: %@ <end>", retString);
}
else if ([data length]==0 && connectionError ==nil)
{NSLog(@"Nothing was downloaded!");
}
else if (connectionError != nil){
NSLog(@"Error happend = %@", connectionError);
}
}];
});
});
}
您没有初始化self.connectionData
。因此,您需要在开头添加以下内容:
if (!self.connectionData) {
self.connectionData = [[NSMutableData alloc] init];
}