-3
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"CRN_JSON"
                                                     ofType:@"json"];


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"]];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES];

});

运行此代码后,我的数据变量为零。如果您点击链接,您会看到它是一个 JSON 文件。我已经在本地使用这个完全相同的文件运行了该函数,但它无法在给定的 URL 处获取数据而不会出错。

4

1 回答 1

2

不知道你为什么要嵌套调用URLWithString:

[NSURL URLWithString:[NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"]]];

一旦会做:

[NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"];

此外,您应该使用dataWithContentsOfURL:options:error:这样您就可以看到任何错误。

于 2013-06-02T14:58:13.027 回答