0

我正在尝试创建一个非常简单的应用程序,它连接到一个 URL 并获取该 URL 的内容,在本例中是一个简单的 xml 文档。我的问题是请求似乎永远不会被发送。

我创建了一个从主文件运行的新项目(基础工具)。我已将 NSURLConnection 的委托设置为 self 并实现了必要的委托方法。问题是这些方法永远不会被调用。如果我将 URL 更改为一些虚假的 URL(如下例所示),这并不重要。在我看来,应用程序在没有发送或等待响应的情况下完成启动。难道是我的应用程序为请求创建了一个线程,然后在不等待响应的情况下完成?我知道该类正在运行,因为我从委托方法中获得了诸如“已创建连接”之类的 NSLOG 语句,但没有 NSLOG 语句。

我错过了什么?

#import "NetworkController.h"
@implementation NetworkController

- (id)init
{
    if(self = [super init])
    {

    }   
    return self;
}
- (void)dealloc
{
    NSLog(@"Calling dealloc");
    [super dealloc];
}
- (void) performConnection
{
    NSMutableData *receivedData;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                                       @"http://www.test.php?some variables"]];

    NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
    [url release];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self startImmediately:YES];

    if (!theConnection)
    {
        NSLog(@"Failed to submit request");
    }
    if(theConnection)
    {
        receivedData=[[NSMutableData data] retain];
        NSLog(@"Created connection.");

        NSLog(@"--------- Request submitted ---------");
        NSLog(@"receivedData: %@", receivedData);
    }

    [connectionRequest release];
}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Received response: %@", response);
    NSLog(@"Received response, connection retain count is %d",[connection retainCount]);
}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
}

#pragma mark NetworkController Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"finished connection retain count:  %d", [connection retainCount]);}

#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error receiving response: %@", error);
    [connection release];
}

@end

#import <Foundation/Foundation.h>
#import "NetworkController.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


    NetworkController *n = [[NetworkController alloc] init];
    n.performConnection;
    [n release];

    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}
4

1 回答 1

2

首先,消息语法是:

[n performConnection];

您所拥有的是属性访问语法。而且由于该方法的类型为返回void,因此即使在该级别上,我也不能保证它可以工作。

无论如何,主要问题是您正在使用异步 NSURLConnection API(好)但期望同步行为(坏)。所有的 NSURLConnection 方法立即返回,然后在后台工作,想法是在事情发生时将委托消息发送到您的网络控制器 - 但是在您完成所有设置后,您会立即释放网络控制器(反过来泄漏连接,因为您release在你的方法中没有dealloc),然后退出。

您需要在释放网络控制器之前运行运行循环,以给连接时间以发送请求、读取响应并向网络控制器发送一些委托消息。你还需要cancelrelease网络控制器的dealloc方法连接。

此外,您需要将该 NSMutableData 变量设置为实例变量,因为您需要能够从委托方法中访问它。不要忘记在您的dealloc方法中也释放它(并将release其设置为nil指示连接结束的两个委托方法)。

于 2009-05-10T13:52:03.310 回答