Hi all Nob Question... of the day.
I'd like to take an array of High Score Objects, and send them all as one JSON Array. I can do this right?
Here's what I have so far. I'm definitely confused as the code probably tells, and not sure I'm even approaching this correctly..
Here's the output:
I'd like to take an array of High Score Objects, and send them all as one JSON Array. I can do this right?
Here's what I have so far. I'm definitely confused as the code probably tells, and not sure I'm even approaching this correctly..
Code:
-(void) sendQuedItemsNow
{
NSLog(@"Sending Qued Data");
NSURL *url = [NSURL URLWithString:@"http://localhost"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
// SingletonCentre is a singleton that holds the mutable array myQuedHighScores
// which holds HighScoreKeeper objects I want to send //
SingletonCentre *sharedHSData = [SingletonCentre sharedSingleton];
NSMutableArray* sendAllJSONDICT = [[NSMutableArray alloc]init];
NSLog(@"Found Qued HighScores: %i",[sharedHSData.myQuedHighScores count]);
for (HighScoreKeeper *myHighscoreToSend in sharedHSData.myQuedHighScores ) {
NSLog(@"name: %@ time: %@ gametime: %i",myHighscoreToSend.name,myHighscoreToSend.gameTime,
myHighscoreToSend.gameTimeSeconds);
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
myHighscoreToSend.name,@"userName",
myHighscoreToSend.gameTime,@"userID",
[NSNumber numberWithInt:myHighscoreToSend.gameTimeSeconds],@"score",
nil];
[sendAllJSONDICT addObject:params];
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:sendAllJSONDICT options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonData sending string:\n%@", jsonString);
// ***What do I do now.???? Defiantly where I'm stuck !! **** //
NSDictionary *paramsit = [NSDictionary dictionaryWithObjectsAndKeys:@{jsonString,nil}];
[httpClient postPath:@"/registar_working_score.php" parameters:paramsit success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful, response '%@'", responseStr);
sharedHSData.queuedItems--;
// if the HighScore was sent then return YES if not NO //
sharedHSData.errorUploading=NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"OKTOUPDATEHIGHSCORES" object:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
sharedHSData.errorUploading=YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"SHOWMETHESTATUSE" object:nil];
}];
}
Here's the output:
Code:
2013-10-12 15:46:59.294 UIScrollView-Paging-networked[3567:207] Sending Qued Data
2013-10-12 15:46:59.301 UIScrollView-Paging-networked[3567:207] Found Qued HighScores: 4
2013-10-12 15:46:59.302 UIScrollView-Paging-networked[3567:207] name: UghBugs time: 00:08:01 gametime: 481
2013-10-12 15:46:59.302 UIScrollView-Paging-networked[3567:207] name: 1 UghBugs time: 00:08:02 gametime: 0
2013-10-12 15:46:59.302 UIScrollView-Paging-networked[3567:207] name: 2 UghBugs time: 00:08:03 gametime: 0
2013-10-12 15:46:59.302 UIScrollView-Paging-networked[3567:207] name: 3 UghBugs time: 00:08:32 gametime: 0
2013-10-12 15:46:59.303 UIScrollView-Paging-networked[3567:207] jsonData sending string:
[
{
"userName" : "UghBugs",
"userID" : "00:08:01",
"score" : 481
},
{
"userName" : "1 UghBugs",
"userID" : "00:08:02",
"score" : 0
},
{
"userName" : "2 UghBugs",
"userID" : "00:08:03",
"score" : 0
},
{
"userName" : "3 UghBugs",
"userID" : "00:08:32",
"score" : 0
}
]
2013-10-12 15:46:59.304 UIScrollView-Paging-networked[3567:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did you forget to nil-terminate your parameter list?'
Last edited: