http 同步请求有效,但相同的异步请求无效
http sync request works, but same request async does not
我知道我在这里遗漏了一些非常简单的东西。
我正在尝试做一个简单的异步 http 请求。如果我在浏览器中输入 URL,我会得到预期的响应。
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",action]];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:convURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSLog(@"received data %@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
这返回了与我在浏览器中看到的相同的 JSON 字符串。
所以现在我试试....
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",action]];
NSURLRequest *request = [NSURLRequest requestWithURL:convURL];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
我的应用程序中有基础库。我在 viewcontroller.h 文件中导入 Foundation/Foundation.h。我的 .h 文件中有 NSURLConnectionDelegate。我已将 didReceiveResponse、didReceiveData、didFailWithError 和 connectionDidFinishLoading 委托方法设置为记录它们已被命中并根据需要初始化、附加或记录传入数据。
然而我一无所获。有什么建议吗?
您需要实际开始连接。
NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
这是一些documentation
处理此问题的正确方法是发送异步连接 - 它提供回调。与 returning 来自同步连接的数据不同,回调将为您提供数据。它不会 return 数据的原因是因为它在引擎盖下产生另一个线程来为你处理它,远离你的代码现在是 运行 的线程(这就是它异步的原因,无论哪个生成的线程保持 运行 向前,而不是等待对 return 的网络调用)。一旦调用 returns,它就会执行回调中的代码。
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//Your code here
}
如果不查看您的所有代码,很难确定确切的问题出在哪里。我的猜测是可能没有实例变量来保存接收到的数据或者变量没有初始化。以下是对我有用的:
@property (nonatomic,retain) NSMutableData *data;
-(void)viewDidLoad{
[super viewDidLoad];
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",@"A"]];
NSURLRequest *request = [NSURLRequest requestWithURL:convURL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
_data = [NSMutableData data];
}
else
{
NSLog(@"The Connection is NULL");
}
}
#pragma mark - Connection methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Displaying the error message to the log.
NSLog(@"Error description: %@",error.description);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Asynchronous done. Received Bytes: %lu", (unsigned long)[_data length]);
NSString *dataString = [[NSString alloc] initWithBytes: [_data mutableBytes] length:[_data length] encoding:NSUTF8StringEncoding];
NSLog(@"\n\nResponse: \n%@ \n", dataString);
}
我知道我在这里遗漏了一些非常简单的东西。 我正在尝试做一个简单的异步 http 请求。如果我在浏览器中输入 URL,我会得到预期的响应。
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",action]];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:convURL];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
NSLog(@"received data %@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
这返回了与我在浏览器中看到的相同的 JSON 字符串。
所以现在我试试....
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",action]];
NSURLRequest *request = [NSURLRequest requestWithURL:convURL];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
我的应用程序中有基础库。我在 viewcontroller.h 文件中导入 Foundation/Foundation.h。我的 .h 文件中有 NSURLConnectionDelegate。我已将 didReceiveResponse、didReceiveData、didFailWithError 和 connectionDidFinishLoading 委托方法设置为记录它们已被命中并根据需要初始化、附加或记录传入数据。
然而我一无所获。有什么建议吗?
您需要实际开始连接。
NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
这是一些documentation
处理此问题的正确方法是发送异步连接 - 它提供回调。与 returning 来自同步连接的数据不同,回调将为您提供数据。它不会 return 数据的原因是因为它在引擎盖下产生另一个线程来为你处理它,远离你的代码现在是 运行 的线程(这就是它异步的原因,无论哪个生成的线程保持 运行 向前,而不是等待对 return 的网络调用)。一旦调用 returns,它就会执行回调中的代码。
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//Your code here
}
如果不查看您的所有代码,很难确定确切的问题出在哪里。我的猜测是可能没有实例变量来保存接收到的数据或者变量没有初始化。以下是对我有用的:
@property (nonatomic,retain) NSMutableData *data;
-(void)viewDidLoad{
[super viewDidLoad];
NSURL *convURL=[NSURL URLWithString:[NSString stringWithFormat:@"http://54.186.212.144/API/index.php?action=%@&key=iOSAPIKey",@"A"]];
NSURLRequest *request = [NSURLRequest requestWithURL:convURL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(connection)
{
_data = [NSMutableData data];
}
else
{
NSLog(@"The Connection is NULL");
}
}
#pragma mark - Connection methods
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_data setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//Displaying the error message to the log.
NSLog(@"Error description: %@",error.description);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Asynchronous done. Received Bytes: %lu", (unsigned long)[_data length]);
NSString *dataString = [[NSString alloc] initWithBytes: [_data mutableBytes] length:[_data length] encoding:NSUTF8StringEncoding];
NSLog(@"\n\nResponse: \n%@ \n", dataString);
}