1

我有 5 个字符串的文本文件。我需要使用 NSURLConnection 来获取这个文件的内容。但是 NSLog 告诉我,“转储”是空的。如何将数据从 NSMutableData 转换为 NSArray。数组是因为我需要在 TableView 中显示这 5 个项目。

NSURLRequest *theRequest=[NSURLRequest
                         requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"]
                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                         timeoutInterval:60.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [NSMutableData data];
    NSString *dump = [[NSString alloc] initWithData:receivedData
                         encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    NSArray *outputArray=[dump componentsSeparatedByString:@"\n"];
    self.namesArray = outputArray;

提前致谢。BTW URL 有效,您可以看到该文件。

4

4 回答 4

2

以下是使用委托实施此解决方案的方法:

在您的 .h 文件中:

@interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSArray *namesArray;

@end

在你的 .m 文件中:

@implementation MyClass

@synthesize receivedData = _receivedData;
@synthesize namesArray = _namesArray;

- (id)init {
    self = [super init];
    if (self) {
        self.receivedData = [NSMutableData data];
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        [connection start];

    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Received response! %@", response);
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *dump = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    self.namesArray = [dump componentsSeparatedByString:@"\n"];
}

@end
于 2012-03-08T14:59:46.290 回答
1

如果不想使用委托,可以使用 NSURLConnection 的同步调用,如下所示:

NSURLRequest *theRequest=[NSURLRequest
                     requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"]
                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                     timeoutInterval:60.0];

NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:theRequest response:&response error:&error];

if (error == nil) {
    NSString *dump = [[NSString alloc] initWithData:receivedData
                     encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    NSArray *outputArray=[dump componentsSeparatedByString:@"\n"];
    self.namesArray = outputArray;
}

请注意,这不会异步运行。如果您不希望它在主线程上运行并阻塞您的主线程/UI,请考虑使用单独的线程来执行该代码或使用 GCD。

于 2012-03-08T13:48:51.563 回答
0

您必须使用委托,然后将接收到的数据保存到 receivedData (现在当然是空的..您刚刚初始化它。)然后您将数据转换为字符串,就像您在示例中所做的那样。看一下NSURLConnectionDelegate

于 2012-03-08T13:33:29.193 回答
0

您需要实现 NSURLConnection 的委托方法,以便收到传入数据的通知。您正在使用异步方法。

另请注意,[NSMutableData data]只是创建一个空的数据对象..所以你不能指望它包含任何数据..

我建议您阅读https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE (完全!)

于 2012-03-08T13:33:59.847 回答