Objective C 接收数据并将其保存为 xml 文件,以便在没有互联网连接的情况下读取它

发布于 2024-11-28 04:11:21 字数 2628 浏览 0 评论 0原文

我有这种方法可以从谷歌阅读器获取带有提要阅读器内容的 xml 文件。 我想在本地保存数据(我确定数据是一个xml文件)我该怎么办??? 我可以将数据存储在本地以便稍后在没有互联网连接的情况下读取吗?我该怎么办?谢谢

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    NSLog(@"------------------------------- connectionDidReceiveResponse");
    expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
    URLresponse = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
    //float l = [responseData length];
    //[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];


    //TESTING
    NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"the  data %@",string);


    [self.responseData appendData:data];
}

编辑**

我使用此代码但不起作用

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    NSLog(@"------------------------------- connectionDidReceiveResponse");
    expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
    URLresponse = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
    //float l = [responseData length];
    //[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];


    //TESTING
    NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"the  data %@",string);
    NSString *path=@"Library/News";
    [data writeToFile:path atomically:YES];

    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
{
    NSLog(@"------------------------------- connectionDidSendBodyData: %d", totalBytesWritten);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)theError
{
    NSLog(@"------------------------------- connectionDidFailWithError: %@", [theError localizedDescription]);

    self.responseData = nil;
    NSString *path=@"Library/News";
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    [self.responseData appendData:data];

    [delegate GoogleReaderRequestDidFailWithError:theError];
}

I have this method to get xml file from google reader with the contens of the feeds reader.
I want to save locally the data (I'm sure that data is an xml file) how can I do???
I can store locally data to read it later without internet connection? HOw can I do? thank

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    NSLog(@"------------------------------- connectionDidReceiveResponse");
    expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
    URLresponse = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
    //float l = [responseData length];
    //[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];


    //TESTING
    NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"the  data %@",string);


    [self.responseData appendData:data];
}

EDIT **

I USE THIS CODE BUT DOESN'T WORK

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    NSLog(@"------------------------------- connectionDidReceiveResponse");
    expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
    URLresponse = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSLog(@"------------------------------- connectionDidReceiveData: %@", data);
    //float l = [responseData length];
    //[delegate GoogleReaderRequestReceiveBytes:l onTotal:[expectedResponseLength floatValue]];


    //TESTING
    NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"the  data %@",string);
    NSString *path=@"Library/News";
    [data writeToFile:path atomically:YES];

    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
{
    NSLog(@"------------------------------- connectionDidSendBodyData: %d", totalBytesWritten);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)theError
{
    NSLog(@"------------------------------- connectionDidFailWithError: %@", [theError localizedDescription]);

    self.responseData = nil;
    NSString *path=@"Library/News";
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    [self.responseData appendData:data];

    [delegate GoogleReaderRequestDidFailWithError:theError];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-12-05 04:11:21

您可以使用 writeToFile:atomically: 方法来存储 NSData 对象。

样本:

[data writeToFile:path atomically:YES];

You can use writeToFile:atomically: method to store your NSData object.

Sample:

[data writeToFile:path atomically:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文