0

我已经查看了NSURLConnectionDelegate connection:didReceiveData not working,但似乎没有任何好的结果,所以我很好奇为什么我无法获取任何数据。

我在didReceiveResponseand中设置了断点didReceiveData

它确实打印出“连接成功”,所以我知道连接已启动。

我正在使用 ARC 进行内存管理。

- (void)load {
        request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn) {
        [conn start];
        NSLog(@"connection succeeded, %s", [myURL description]);  
        responseData = [NSMutableData data];
    } else {
        NSLog(@"connection failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

更新:

要了解我如何测试它,请查看SenTestCase 未调用异步单元测试

我确实实现了 jonkroll 提到的两种方法,在他的回答中,我只是没有展示它们,但是,它们也没有被调用。

我添加[conn start]只是因为它不起作用,我希望这可以解决它,但没有这样的运气。

4

2 回答 2

3

当您像这样声明您的连接时:

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

您正在创建一个本地指针。当你的方法完成时,因为它是对 的最后一个强引用,所以NSURLConnection释放ARC它。您需要使用强大的 ivar(和/或)属性来保持对NSURLConnection您创建的强大引用。

编辑

这是我在示例项目中测试的基本代码示例。试一试。详细日志记录会有所帮助。

@implementation <#Your class here#> {
    // With ARC ivars are strong by default 
    NSMutableData *_downloadedData;
    NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
    if (realResponse.statusCode == 200){ 
        // Really any 2** but for example
        _downloadedData = [[NSMutableData alloc] init];
        NSLog(@"Good response");
    } else {
        NSLog(@"Bad response = %i",realResponse.statusCode);
    }
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if (connection == _connection){
        [_downloadedData appendData:data];
        NSLog(@"Getting data...");
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if (connection == _connection){
        _connection = nil;
        NSLog(@"We're done, inform the UI or the delegates");
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    _connection = nil;
    NSLog(@"Oh no! Error:%@",error.localizedDescription);
}
- (void)load {
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                      cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                  timeoutInterval:60];
    // Assign strong pointer to new connection
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"Connection was initialized? = %@",(!!_connection)?@"YES":@"NO");
}
@end
于 2012-04-18T01:49:31.713 回答
0

NSURLConnection 方法initWithRequest从 url 开始对数据的异步请求。因为请求是异步完成的,所以您不能期望以调用请求的相同方法处理响应。相反,您需要在 NSURLConnection 的委托回调方法中这样做。您已经实现了didReceiveResponse:and didReceiveData:,但还有其他一些对您有用的。

如果你想查看响应的内容,你应该这样做connectionDidFinishLoading:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // response is complete, do something with the data
    NSLog(@"%@", responseData);
}

您的代码打印出“连接成功”这一事实并不意味着请求成功,只是表示成功创建了 NSURLConnection 对象。要测试连接是否有问题,您可以实现委托方法connection:didFailWithError:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

也没有必要打电话[conn start]。当您调用时,请求将自动启动initWithRequest:

我建议阅读 Apple 关于Using NSURLConnection的文档以获取更多详细信息。

于 2012-04-18T01:26:40.950 回答