【问题标题】:NSURL Connection will/won't load dataNSURL 连接将/不会加载数据
【发布时间】:2011-01-27 09:16:16
【问题描述】:

这是我的问题:

我几乎按照 iPhone 开发者文档中的 NSURLConnection tutorial 到了 T,它只是有点工作。

这就是问题所在:
该对象似乎已正确创建并委托给 connectionDidFinishLoading,但是对于我尝试加载的任何 URL,响应数据总是最终只有 0 个字节。如果这有什么不同,我正在模拟器中运行。

这是我的相关代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"WVFS Player";

    //create a request
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wvfs.josh-kaplan.com/nowPlaying.php"]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:60.0];
    // create a connection
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {
        // create the datum
        responseData=[[NSMutableData data] retain];
    } else {
        // code this later
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength:0];
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // make it work
    NSLog(@"Succeeded! Received %d bytes of data:",[responseData length]);

    // release it
    [connection release];
    [responseData release];
}

这是我的日志输出:

[Session started at 2010-03-14 09:01:09 -0400.]
2010-03-14 09:01:14.784 WVFS[19571:207] Succeeded! Received 0 bytes of data:

有什么想法吗?

【问题讨论】:

    标签: iphone objective-c iphone-sdk-3.0 ios-simulator nsurlconnection


    【解决方案1】:

    你忘了实现connection:didReceiveData:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2011-02-12
      • 1970-01-01
      相关资源
      最近更新 更多