I have been working on my first app for some time now, but it seems that I am missing something here. The code below was originally only in my app delegate; I just added a refresh button in the first view controller in order to refresh the data the app uses, so my first idea was to call the app delegate code again inside the view controller, but the NSURLConnection delegate methods did not fire. Now I have added the code to the view controller (and also kept it in the app delegate) and the delegate methods fire when the refresh button is pressed, but responseData is null, refreshString is empty and JSONValue throws an “Unexpected end of input” error (because nothing is being inputted) in the log (the app still runs and clears the UITableView, this is proper behavior since the array is now empty). The JSON data’s character set is UTF-8
There may be a better place for this code where it can be kept in one place and run as needed, and if there are any suggestions regarding that I would appreciate them as well, but my primary concern is figuring out why this code works correctly the first time, but does not work the second.
- (void) refreshData{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://beersandears.net/beer-list-json.php?native=yes"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"loadData complete");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
NSLog(@"Response received");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
NSLog(@"Data received");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
//label.text = [NSString stringWithFormat:@"Connection failed %@", [error description]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
connection = nil;
NSString *refreshString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSDictionary *results = [refreshString JSONValue];
BeerListAppDataObject* theDataObject = [self beerListAppDataObject];
[theDataObject.beerList removeAllObjects];
[theDataObject.mapList removeAllObjects];
[theDataObject.blogList removeAllObjects];
theDataObject.beerList = [results objectForKey:@"beer"];
theDataObject.mapList = [results objectForKey:@"map"];
theDataObject.blogList = [results objectForKey:@"blog"];
NSLog(@"Data Loaded");
[self.tableView reloadData];
}
You’re releasing the NSData responseData member variable in finishLoading:
But, I don’t see anything else getting another NSData object to append.
In this sample code here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
when they start the connection again (in your refreshMethod), they do:
The key line being this where they get an NSMutabaleData object and retain it.