我正在构建一个简单的 ios 应用程序,它试图显示来自 flickr 的图像序列。问题是它有时有效,有时失败,我不确定哪里出错了。应用程序具有主详细信息视图。当前未使用 Detailview,问题发生在主视图本身。我在 masterview 上设置了一个刷新按钮,并在用户尝试刷新时尝试重新获取数据。
github:https ://github.com/kiranjulapalli/flickrexcercise
欣赏任何见解。
这是错误:
[MasterViewController connectionDidFinishLoading:] JSON error: The operation couldn’t be completed. (Cocoa error 3840.)
这是代码:
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
执行:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(fetchFlickrData)];
self.navigationItem.rightBarButtonItem = addButton;
[self fetchFlickrData];
}
- (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 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"previewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSDictionary* item = [items objectAtIndex:[indexPath row]];
NSString* url = [[item objectForKey:@"media"] objectForKey:@"m"];
[cell.photo setImageWithURL:[NSURL URLWithString:url]
placeholderImage:[UIImage imageNamed:@"thumbnail.jpg"]];
return cell;
}
//获取数据
-(bool)fetchFlickrData {
// build the url for fetching json data
NSString* url = [NSString stringWithFormat:@"%@", FLICKR_PUBLIC_API_URI];
// Escape the characters in the URL
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
connection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (connection)
{
self.receivedData = [[NSMutableData alloc]init];
}
else
{ // Failed connection
return false;
}
return true;
}
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
[self.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
receivedData = nil;
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
--
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
NSString* stringcontents = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSData *jsonData = [stringcontents dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *tempresults = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
if (error) {
// *代码从上面继续://这里是我看到错误的地方,并且大部分时间都发生了//日志表明数据正常
NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription);
NSLog(@"%@", stringcontents);
}
if ( !error )
{
NSDictionary* tempitems = [tempresults objectForKey:@"items" ];
if ( !items )
{ // Allocate the mutable array if not already allocated
items = [[NSMutableArray alloc]init];
}
else
{
[items removeAllObjects];
}
for (NSDictionary* item in tempitems)
{
[items addObject:item];
}
[self.tableView reloadData];
}
else
{
//Alert error
}
self.connection =nil;
}
抛出错误时的内容:
{
"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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (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": "nobody@flickr.com (jj_delrosario)",
"author_id": "57743051@N03",
"tags": "square squareformat iphoneography instagramapp uploaded:by=instagram foursquare:venue=4b7e974ef964a5203df42fe3"
}
]
}