ios - 在 ios uitableview 中间歇性地获取 json 提要失败
<p><p>我正在构建一个简单的 ios 应用程序,它试图显示来自 flickr 的图像序列。问题是它有时有效,有时失败,我不确定哪里出错了。
应用程序具有主详细信息 View 。当前未使用 Detailview,问题发生在主视图本身。我在 masterview 上设置了一个刷新按钮,并在用户尝试刷新时尝试重新获取数据。</p>
<p>github:<a href="https://github.com/kiranjulapalli/flickrexcercise" rel="noreferrer noopener nofollow">https://github.com/kiranjulapalli/flickrexcercise</a> </p>
<p>欣赏任何见解。</p>
<p>这是错误:</p>
<pre><code> JSON error: The operation couldn’t be completed. (Cocoa error 3840.)
</code></pre>
<p>代码如下:</p>
<pre><code>Header MasterViewController.h:
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController
@property (strong, nonatomic) NSMutableData* receivedData;
@property (strong, nonatomic) NSURLConnection *connection;
@property (strong, nonatomic) NSMutableArray* items;
@end
</code></pre>
<p>实现:</p>
<pre><code>- (void)viewDidLoad
{
;
UIBarButtonItem *addButton = [ initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(fetchFlickrData)];
self.navigationItem.rightBarButtonItem = addButton;
;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if ( items != nil )
{
return items.count;
}
else
{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"previewCell";
previewCell *cell = ;
if (cell == nil)
{
NSArray *nib = [ loadNibNamed:@"previewCell" owner:self options:nil];
cell = ;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDictionary* item = ];
NSString* url = [ objectForKey:@"m"];
placeholderImage:];
return cell;
}
</code></pre>
<p>//获取数据</p>
<pre><code>-(bool)fetchFlickrData {
// build the url for fetching json data
NSString* url = ;
// Escape the characters in the URL
url = ;
// Create the request.
NSURLRequest *theRequest=
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
connection=[ initWithRequest:theRequest delegate:self];
if (connection)
{
self.receivedData = [init];
}
else
{ // Failed connection
return false;
}
return true;
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
;
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
;
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
receivedData = nil;
NSLog(@"Connection failed! Error - %@ %@",
,
[ objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
</code></pre>
<p>--</p>
<pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",);
NSString* stringcontents = [ initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSData *jsonData = ;
NSError *error = nil;
NSDictionary *tempresults = jsonData ? : nil;
if (error) {
</code></pre>
<p>//*代码从上面继续:
//这里是我看到错误的地方,而且大部分时间都发生了
//日志表明数据正常</p>
<pre><code> NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass(), NSStringFromSelector(_cmd), error.localizedDescription);
NSLog(@"%@", stringcontents);
}
if ( !error )
{
NSDictionary* tempitems = ;
if ( !items )
{ // Allocate the mutable array if not already allocated
items = [init];
}
else
{
;
}
for (NSDictionary* item in tempitems)
{
;
}
;
}
else
{
//Alert error
}
self.connection =nil;
}
</code></pre>
<p>抛出错误时的内容:</p>
<pre><code>{
"title": "Uploads from everyone",
"link": "http://www.flickr.com/photos/",
"description": "",
"modified": "2013-02-24T05:49:14Z",
"generator": "http://www.flickr.com/",
"items": [
{
"title": "P1610143",
"link": "http://www.flickr.com/photos/jungandyu/8501768895/",
"media": {"m":"http://farm9.staticflickr.com/8094/8501768895_2e05d3d266_m.jpg"},
"date_taken": "2013-02-16T12:20:10-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/jungandyu/\">Bighead City<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/jungandyu/8501768895/\" title=\"P1610143\"><img src=\"http://farm9.staticflickr.com/8094/8501768895_2e05d3d266_m.jpg\" width=\"240\" height=\"180\" alt=\"P1610143\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:14Z",
"author": "[email protected] (Bighead City)",
"author_id": "19809616@N08",
"tags": ""
},
{
"title": "20121231-DSC08686.jpg",
"link": "http://www.flickr.com/photos/wasicat/8501768957/",
"media": {"m":"http://farm9.staticflickr.com/8385/8501768957_23f8a2fdcc_m.jpg"},
"date_taken": "2012-12-31T12:52:35-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/wasicat/\">William0912<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/wasicat/8501768957/\" title=\"20121231-DSC08686.jpg\"><img src=\"http://farm9.staticflickr.com/8385/8501768957_23f8a2fdcc_m.jpg\" width=\"240\" height=\"180\" alt=\"20121231-DSC08686.jpg\" /><\/a><\/p> <p>201301015鎮西堡跨年<br /> 明新登山社<\/p>",
"published": "2013-02-24T05:49:16Z",
"author": "[email protected] (William0912)",
"author_id": "81613772@N00",
"tags": "鎮西堡 friendlyflickr 201301015鎮西堡跨年 明新登山社"
},
{
"title": "IMAG0710",
"link": "http://www.flickr.com/photos/84380329@N08/8501769085/",
"media": {"m":"http://farm9.staticflickr.com/8231/8501769085_1433e589fe_m.jpg"},
"date_taken": "2012-10-27T22:30:28-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/84380329@N08/\">bmota2007<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/84380329@N08/8501769085/\" title=\"IMAG0710\"><img src=\"http://farm9.staticflickr.com/8231/8501769085_1433e589fe_m.jpg\" width=\"135\" height=\"240\" alt=\"IMAG0710\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:20Z",
"author": "[email protected] (bmota2007)",
"author_id": "84380329@N08",
"tags": ""
},
{
"title": "IMAG0157.jpg",
"link": "http://www.flickr.com/photos/58459890@N08/8501769129/",
"media": {"m":"http://farm9.staticflickr.com/8532/8501769129_8becb3ccae_m.jpg"},
"date_taken": "2012-09-08T23:55:12-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/58459890@N08/\">k7801101<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/58459890@N08/8501769129/\" title=\"IMAG0157.jpg\"><img src=\"http://farm9.staticflickr.com/8532/8501769129_8becb3ccae_m.jpg\" width=\"160\" height=\"240\" alt=\"IMAG0157.jpg\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:22Z",
"author": "[email protected] (k7801101)",
"author_id": "58459890@N08",
"tags": ""
},
{
"title": "The Press - Work at Rockville Metro Plaza I",
"link": "http://www.flickr.com/photos/nakevanc/8501769159/",
"media": {"m":"http://farm9.staticflickr.com/8234/8501769159_c68af16b82_m.jpg"},
"date_taken": "2013-02-24T00:45:53-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/nakevanc/\">Nakeva<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/nakevanc/8501769159/\" title=\"The Press - Work at Rockville Metro Plaza I\"><img src=\"http://farm9.staticflickr.com/8234/8501769159_c68af16b82_m.jpg\" width=\"240\" height=\"180\" alt=\"The Press - Work at Rockville Metro Plaza I\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:22Z",
"author": "[email protected] (Nakeva)",
"author_id": "28116601@N02",
"tags": ""
},
{
"title": "BHS BBall Region Quarterfinals_1077.JPG",
"link": "http://www.flickr.com/photos/kbaumann/8501769163/",
"media": {"m":"http://farm9.staticflickr.com/8231/8501769163_3e54304c40_m.jpg"},
"date_taken": "2013-02-23T18:32:15-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/kbaumann/\">kjbaumann<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/kbaumann/8501769163/\" title=\"BHS BBall Region Quarterfinals_1077.JPG\"><img src=\"http://farm9.staticflickr.com/8231/8501769163_3e54304c40_m.jpg\" width=\"159\" height=\"240\" alt=\"BHS BBall Region Quarterfinals_1077.JPG\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:22Z",
"author": "[email protected] (kjbaumann)",
"author_id": "33453969@N04",
"tags": ""
},
{
"title": "DSC_1458",
"link": "http://www.flickr.com/photos/eddilui/8501769217/",
"media": {"m":"http://farm9.staticflickr.com/8099/8501769217_43f83ea28b_m.jpg"},
"date_taken": "2013-01-09T23:43:06-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/eddilui/\">Eddi Lui<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/eddilui/8501769217/\" title=\"DSC_1458\"><img src=\"http://farm9.staticflickr.com/8099/8501769217_43f83ea28b_m.jpg\" width=\"240\" height=\"160\" alt=\"DSC_1458\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:24Z",
"author": "[email protected] (Eddi Lui)",
"author_id": "68014013@N08",
"tags": ""
},
{
"title": "DSC_0112",
"link": "http://www.flickr.com/photos/pjkdmd/8501769273/",
"media": {"m":"http://farm9.staticflickr.com/8233/8501769273_63a9bb7206_m.jpg"},
"date_taken": "2013-02-23T22:00:09-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/pjkdmd/\">pjkdmd<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/pjkdmd/8501769273/\" title=\"DSC_0112\"><img src=\"http://farm9.staticflickr.com/8233/8501769273_63a9bb7206_m.jpg\" width=\"240\" height=\"159\" alt=\"DSC_0112\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:25Z",
"author": "[email protected] (pjkdmd)",
"author_id": "81759199@N08",
"tags": ""
},
{
"title": "8499469359_a854f58ba3_z",
"link": "http://www.flickr.com/photos/ury94/8501769293/",
"media": {"m":"http://farm9.staticflickr.com/8093/8501769293_f19a12de4f_m.jpg"},
"date_taken": "2013-02-24T06:49:26-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/ury94/\">Hồng Anh ®<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/ury94/8501769293/\" title=\"8499469359_a854f58ba3_z\"><img src=\"http://farm9.staticflickr.com/8093/8501769293_f19a12de4f_m.jpg\" width=\"160\" height=\"240\" alt=\"8499469359_a854f58ba3_z\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:26Z",
"author": "[email protected] (Hồng Anh ®)",
"author_id": "39951352@N03",
"tags": ""
},
{
"title": "Punggol End",
"link": "http://www.flickr.com/photos/eideticme/8502873874/",
"media": {"m":"http://farm9.staticflickr.com/8242/8502873874_790c93ed5b_m.jpg"},
"date_taken": "2012-05-26T23:29:17-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/eideticme/\">eidetic.me<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/eideticme/8502873874/\" title=\"Punggol End\"><img src=\"http://farm9.staticflickr.com/8242/8502873874_790c93ed5b_m.jpg\" width=\"240\" height=\"160\" alt=\"Punggol End\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:14Z",
"author": "[email protected] (eidetic.me)",
"author_id": "76188829@N02",
"tags": ""
},
{
"title": "IMG_0597",
"link": "http://www.flickr.com/photos/mediaocu/8502873916/",
"media": {"m":"http://farm9.staticflickr.com/8381/8502873916_a9cb6e726a_m.jpg"},
"date_taken": "2013-02-23T04:39:39-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/mediaocu/\">MediaOCU<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/mediaocu/8502873916/\" title=\"IMG_0597\"><img src=\"http://farm9.staticflickr.com/8381/8502873916_a9cb6e726a_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_0597\" /><\/a><\/p> <p>The OCU spirit squad performed at a spirit competition in the Henry J Freede activity and wellness center on Saturday afternoon.<br /> <br /> The Campus/Michael Horn<\/p>",
"published": "2013-02-24T05:49:16Z",
"author": "[email protected] (MediaOCU)",
"author_id": "86702597@N04",
"tags": ""
},
{
"title": "Squaw Valley ski trip",
"link": "http://www.flickr.com/photos/hallucinant/8502873920/",
"media": {"m":"http://farm9.staticflickr.com/8391/8502873920_e207276293_m.jpg"},
"date_taken": "2013-02-22T10:30:59-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/hallucinant/\">hallucinant<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/hallucinant/8502873920/\" title=\"Squaw Valley ski trip\"><img src=\"http://farm9.staticflickr.com/8391/8502873920_e207276293_m.jpg\" width=\"240\" height=\"160\" alt=\"Squaw Valley ski trip\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:16Z",
"author": "[email protected] (hallucinant)",
"author_id": "62641951@N00",
"tags": ""
},
{
"title": "053/365",
"link": "http://www.flickr.com/photos/originalrocker/8502873948/",
"media": {"m":"http://farm9.staticflickr.com/8385/8502873948_06c8800be5_m.jpg"},
"date_taken": "2013-02-22T19:37:35-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/originalrocker/\">originalrocker<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/originalrocker/8502873948/\" title=\"053/365\"><img src=\"http://farm9.staticflickr.com/8385/8502873948_06c8800be5_m.jpg\" width=\"240\" height=\"180\" alt=\"053/365\" /><\/a><\/p> <p>worst feeling ever: when your little one is feeling bad with a high fever, and you can only do so much. :(<\/p>",
"published": "2013-02-24T05:49:17Z",
"author": "[email protected] (originalrocker)",
"author_id": "49882128@N00",
"tags": "max project day ruben tracy 365 homies guzman maximiliano 2013 365dayproject uploaded:by=flickrmobile flickriosapp:filter=nofilter"
},
{
"title": "Oh qu\'il est haut, le pont du Bono...",
"link": "http://www.flickr.com/photos/dyc56/8502873962/",
"media": {"m":"http://farm9.staticflickr.com/8239/8502873962_a1851d4140_m.jpg"},
"date_taken": "2013-01-13T16:24:20-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/dyc56/\">DYC56<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/dyc56/8502873962/\" title=\"Oh qu\'il est haut, le pont du Bono...\"><img src=\"http://farm9.staticflickr.com/8239/8502873962_a1851d4140_m.jpg\" width=\"240\" height=\"204\" alt=\"Oh qu\'il est haut, le pont du Bono...\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:17Z",
"author": "[email protected] (DYC56)",
"author_id": "76919092@N08",
"tags": "bridge france art soleil eau lumière pierre bretagne breizh pont bateau morbihan soe hdr bois nikond700 ahhhlafrance nikonpassion pixeliste nikkor1424mmf28g bretagnesanspareil mordusdephotos bouboun56"
},
{
"title": "Playing a rehearsal tomorrow and the lead sheets will be only on my iPad. Lets see how this goes!",
"link": "http://www.flickr.com/photos/jasondegenhardt/8502874026/",
"media": {"m":"http://farm9.staticflickr.com/8245/8502874026_c39b545160_m.jpg"},
"date_taken": "2013-02-23T23:49:19-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/jasondegenhardt/\">jasondeg<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/jasondegenhardt/8502874026/\" title=\"Playing a rehearsal tomorrow and the lead sheets will be only on my iPad. Lets see how this goes!\"><img src=\"http://farm9.staticflickr.com/8245/8502874026_c39b545160_m.jpg\" width=\"240\" height=\"240\" alt=\"Playing a rehearsal tomorrow and the lead sheets will be only on my iPad. Lets see how this goes!\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:19Z",
"author": "[email protected] (jasondeg)",
"author_id": "40834611@N06",
"tags": "square squareformat iphoneography instagramapp uploaded:by=instagram foursquare:venue=50daa303e4b019a9f2725785"
},
{
"title": "IMG_0588",
"link": "http://www.flickr.com/photos/hmclin/8502874044/",
"media": {"m":"http://farm9.staticflickr.com/8383/8502874044_7caebe46c5_m.jpg"},
"date_taken": "2013-02-23T18:01:42-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/hmclin/\">Henry McLin<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/hmclin/8502874044/\" title=\"IMG_0588\"><img src=\"http://farm9.staticflickr.com/8383/8502874044_7caebe46c5_m.jpg\" width=\"240\" height=\"160\" alt=\"IMG_0588\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:20Z",
"author": "[email protected] (Henry McLin)",
"author_id": "95014738@N00",
"tags": ""
},
{
"title": "",
"link": "http://www.flickr.com/photos/n28307/8502874096/",
"media": {"m":"http://farm9.staticflickr.com/8228/8502874096_2494bd2d14_m.jpg"},
"date_taken": "2013-02-23T09:29:12-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/n28307/\">beltz6<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/n28307/8502874096/\" title=\"\"><img src=\"http://farm9.staticflickr.com/8228/8502874096_2494bd2d14_m.jpg\" width=\"240\" height=\"160\" alt=\"\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:21Z",
"author": "[email protected] (beltz6)",
"author_id": "23089307@N02",
"tags": "mesa ucsb universityofcalifornia afsdxvrzoomnikkor18200mmf3556gifedii"
},
{
"title": "bristow_party_124",
"link": "http://www.flickr.com/photos/amyherndonphotography/8502874102/",
"media": {"m":"http://farm9.staticflickr.com/8236/8502874102_7192f5c6f0_m.jpg"},
"date_taken": "2013-02-17T00:16:46-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/amyherndonphotography/\">amy herndon<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/amyherndonphotography/8502874102/\" title=\"bristow_party_124\"><img src=\"http://farm9.staticflickr.com/8236/8502874102_7192f5c6f0_m.jpg\" width=\"180\" height=\"240\" alt=\"bristow_party_124\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:22Z",
"author": "[email protected] (amy herndon)",
"author_id": "62464749@N08",
"tags": ""
},
{
"title": "100_1764",
"link": "http://www.flickr.com/photos/lelikadgi/8502874124/",
"media": {"m":"http://farm9.staticflickr.com/8237/8502874124_9a7a7f356e_m.jpg"},
"date_taken": "2005-08-17T12:46:26-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/lelikadgi/\">lelikadgi<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/lelikadgi/8502874124/\" title=\"100_1764\"><img src=\"http://farm9.staticflickr.com/8237/8502874124_9a7a7f356e_m.jpg\" width=\"180\" height=\"240\" alt=\"100_1764\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:22Z",
"author": "[email protected] (lelikadgi)",
"author_id": "77157992@N04",
"tags": ""
},
{
"title": "@eliseebrown",
"link": "http://www.flickr.com/photos/jj_delrosario/8502874178/",
"media": {"m":"http://farm9.staticflickr.com/8376/8502874178_5c32893a8e_m.jpg"},
"date_taken": "2013-02-24T00:49:24-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/jj_delrosario/\">jj_delrosario<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/jj_delrosario/8502874178/\" title=\"@eliseebrown\"><img src=\"http://farm9.staticflickr.com/8376/8502874178_5c32893a8e_m.jpg\" width=\"240\" height=\"240\" alt=\"@eliseebrown\" /><\/a><\/p> ",
"published": "2013-02-24T05:49:24Z",
"author": "[email protected] (jj_delrosario)",
"author_id": "57743051@N03",
"tags": "square squareformat iphoneography instagramapp uploaded:by=instagram foursquare:venue=4b7e974ef964a5203df42fe3"
}
]
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用 <code>NSLog(@"%@", );</code> 进一步调试问题。</p></p>
<p style="font-size: 20px;">关于ios - 在 ios uitableview 中间歇性地获取 json 提要失败,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/15048955/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/15048955/
</a>
</p>
页:
[1]