2013-12-12 81 views
1

我想每次从服务器保存json文件,当用户有互联网连接时使用它,当iPhone没有互联网连接。但它不起作用。这里是我的代码:保存JSON文件到iPhone

- (void)writeJsonToFile 
{ 
    //applications Documents dirctory path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //live json data url 
    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    //attempt to download live data 
    if (urlData) 
    { 
     NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 
     [urlData writeToFile:filePath atomically:YES]; 
    } 
    //copy data from initial package into the applications Documents folder 
    else 
    { 
     //file to write to 
     NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 

     //file to copy from 
     NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ]; 
     NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil]; 

     //write file to device 
     [jsonData writeToFile:filePath atomically:YES]; 
    } 
} 
+0

你的意思是不工作? –

+0

您是否尝试过使用Core Data来满足您的需求..? – mAc

+0

应用程序崩溃没有互联网,但必须使用本地json文件 – Anton

回答

0

你应该在适当的方式创建路径

替换此

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"]; 
+0

不工作:(以下是控制台数据:2013-12-12 14:30:54.420 iPhone中心[301:1503]网络连接失败,在睡眠6.849714秒后尝试4 。 2013-12-12 14:31:13.962 iPhoneCenter [301:60b] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'数据参数为零' – Anton

+0

然后检查数据是否存在?给出正确路径的解决方案,首先检查是否从WerbService下载数据,如果(data.length> 0)则只写入路径。 –

0

尝试使用NSUserDefaults的,而不是写这篇小JSON文本到文件

- (void)writeJsonToFile 
{ 
    NSString *jsonString; 

    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    if (urlData) 
    { 
     // JSON successfully downloaded, store it to user defaults 
     jsonString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 
     [[NSUserDefaults standardUserDefaults] setValue:jsonString forKey:@"jsonString"]; 
    } 
    else 
    { 
     // no urlData, using stored JSON 
     jsonString = [[NSUserDefaults standardUserDefaults] valueForKey:@"jsonString"]; 
    } 
} 

甚至可能更好:

- (NSString *)getJSON 
{ 
    <the code above> 
    return jsonString; 
} 

,然后用它在其它功能:

NSString *jsonString = [self getJSON]; 
0

安东,在第一时间获取在nsmutable arrayjson数据和存储您阵列保持数据时,你再次运行UR阵列不是一个零,但重新播放数据第二次...和另一种方式来存储数据在local database ...如果你需要存储的数据..

但我们的问题是解决第一个..幸运..

2

试试这个。 。 。

- (void)writeJsonToFile 
{ 
    //applications Documents dirctory path 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    //live json data url 
    NSString *stringURL = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    NSData *urlData = [NSData dataWithContentsOfURL:url]; 

    //attempt to download live data 
    if (urlData) 
    { 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"]; 
     [urlData writeToFile:filePath atomically:YES]; 
    } 
    //copy data from initial package into the applications Documents folder 
    else 
    { 
     //file to write to 
     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"];; 

     //file to copy from 
     NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ]; 
     NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil]; 

     //write file to device 
     [jsonData writeToFile:filePath atomically:YES]; 
    } 
} 
2

这对我有用。阅读AFJSONRequestOperation指南。代码还会检查json文件是否已被缓存。

NSString *path = @"http://volodko.info/ic/json.php"; 
    NSURL *url = [[NSURL alloc] initWithString:path]; 
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 

    id cachedJson = [[NSUserDefaults standardUserDefaults] valueForKey:path]; 
    if (cachedJson) { 
     [self didUpdateJSON:cachedJson]; 
    } else { 
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
      [self didUpdateJSON:JSON]; 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [[NSUserDefaults standardUserDefaults] setObject:JSON forKey:path]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 
      }); 
     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
      NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo); 
     }]; 
     [operation start]; 
    } 
2

从JSON解析器获取数据并将其存储在数组中。之后,您可以将其添加到SQLite数据库。