http同步请求有效,但异步请求不同

时间:2015-02-17 18:07:53

标签: ios ios8.1

我知道我在这里遗漏了一些非常简单的东西。 我正在尝试做一个简单的异步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委托方法设置为记录它们已被命中并初始化,追加或记录传入数据。

然而我一无所获。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

处理此问题的正确方法是发送异步连接 - 它提供回调。与从同步连接返回数据不同,回调将为您提供数据。它不会返回数据的原因是因为它产生了另一个线程来为你处理它,远离你的代码现在运行的线程(这是使它异步的原因,无论哪个线程产生它继续向前运行,而不是等待网络呼叫返回)。一旦调用返回,它就会执行回调中的代码。

 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
//Your code here 
}

答案 1 :(得分:0)

如果没有看到所有代码,很难确定确切问题的位置。我的猜测是,可能没有实例变量来保存接收到的数据,或者变量未初始化。这对我有用:

@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);
}

答案 2 :(得分:-1)

您需要实际开始连接。

NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

以下是documentation