Objective C 接收数据并将其保存为 xml 文件,以便在没有互联网连接的情况下读取它
我有这种方法可以从谷歌阅读器获取带有提要阅读器内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 writeToFile:atomically: 方法来存储 NSData 对象。
样本:
You can use
writeToFile:atomically:
method to store your NSData object.Sample: