2

我对 iOS 开发相当陌生,我正在尝试从 URL 获取内容。我正在使用 Apple 教程来使用 NSURLConnection,一切正常,但我没有获取数据。我环顾四周,找不到我的问题的答案。我也在我的项目中使用ARC,也许这会导致问题?

这是我正在使用的代码,从我的头文件开始:

@interface ViewController : UIViewController
@property (nonatomic, retain) NSMutableData *receivedData;
@end

实现文件:

@implementation ViewController

@synthesize receivedData;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"1. viewDidLoad");
    // Create the request
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    // Create the connection with the request and start loading the data
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        NSLog(@"2. connection succeeded");
        receivedData = [NSMutableData data];
    } else {
        NSLog(@"2. connection failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse     *)response
{
    NSLog(@"3. didReceiveResponse");
    [receivedData setLength:0];
}

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

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"4. Succeeded! Received %d bytes of data", [receivedData length]);
}

所有的 NSLog 都在工作,但它一直说 receivedData 有 0 个字节。我找不到我的问题的答案,所以我希望我能找到这个问题的答案。

4

3 回答 3

0

我已经使用 ARC翻译了URL 加载系统编程指南中的Using NSURLConnection部分。

//
//  MyConnection.h
//

#import <Foundation/Foundation.h>

@interface MyConnection : NSObject

- (void)start;

@property NSMutableData *receivedData;

@end


//
//  MyConnection.m
//

#import "MyConnection.h"

@implementation MyConnection

- (void)start {

    // Create the request
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    // Create the connection with the request and start loading the data
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {
        // Create the NSMUtableData to fold the received data
        // ReceivedData is an instance variable declared elsewhere
        _receivedData = [NSMutableData data];

    }
}

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

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // Inform the user
    NSLog(@"Connectionfailed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Do something with the data
    NSLog(@"Succeeded! Recevied %d bytes of data", [_receivedData length]);

}

@end
于 2013-06-04T09:33:21.420 回答
0

只是为了完成。我遇到了同样的问题,原因是threading。如果拥有NSURLConnection对象的线程与拥有 的线程不同delegatedelegate则不会收到通知。这意味着连接中的错误也将消失。

因此,请确保您NSURLConnection在创建委托的同一线程上创建。

于 2013-07-31T14:21:12.537 回答
-1

您需要启动连接。

例如,您可以使用– initWithRequest:delegate:startImmediately:.

对现有代码的更小的更改是仅- start在 if 分支上的连接上使用:

NSURLConnection *connection = [[NSURLConnection alloc] 
                                initWithRequest:request 
                                       delegate:self 
                               startImmediately:NO
                               ];
if (connection) {
    NSLog(@"2. connection succeeded");
    receivedData = [NSMutableData data];
    [connection start];
} else {
    NSLog(@"2. connection failed");
}

您可以在此处找到 NSURLConnection 的参考手册

于 2012-04-12T06:36:55.667 回答