1
我正在構建一個簡單的ios應用程序,試圖顯示來自flickr的圖像序列。問題是它有時會起作用,有時會失敗,我不確定哪裏出了問題。 應用程序有一個主詳細視圖。目前沒有使用Detailview,並且問題發生在主視圖本身中。我在masterview上設置了一個刷新按鈕,並在用戶嘗試刷新時嘗試重新提取數據。抓取json飼料間歇性地失敗ios uitableview
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) {
// * CODE續從上面: //這裏是我看到的錯誤,並且出現大部分時間 // LOG表示數據是好的
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": "[email protected] (Bighead City)",
"author_id": "[email protected]",
"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": "[email protected]",
"tags": "鎮西堡 friendlyflickr 201301015鎮西堡跨年 明新登山社"
},
{
"title": "IMAG0710",
"link": "http://www.flickr.com/photos/[email protected]/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/[email protected]/\">bmota2007<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/[email protected]/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": "[email protected]",
"tags": ""
},
{
"title": "IMAG0157.jpg",
"link": "http://www.flickr.com/photos/[email protected]/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/[email protected]/\">k7801101<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/[email protected]/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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]4",
"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": "[email protected]",
"tags": "square squareformat iphoneography instagramapp uploaded:by=instagram foursquare:venue=4b7e974ef964a5203df42fe3"
}
]
}
添加'NSLog(@「%@」,stringcontents)'然後崩潰之前的內容是什麼? – Ric 2013-02-24 05:44:25
由於評論不允許我添加,所以我更新了內容失敗時的問題。 – Kiran 2013-02-24 05:51:51
嗯我現在也很難過。 +1 – Ric 2013-02-24 05:59:00