Submitting a New High Score Record to the Simple High-Score Server in Objective-C

Drawer Denso QR Bar Code in Objective-C Submitting a New High Score Record to the Simple High-Score Server

Listing 2-20. Submitting a New High Score Record to the Simple High-Score Server
Encode Quick Response Code In Objective-C
Using Barcode encoder for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
Code39 Generator In Objective-C
Using Barcode encoder for iPhone Control to generate, create Code-39 image in iPhone applications.
www.OnBarcode.com
NSData *highScoreData = [[NSString stringWithFormat: @"<high-score> \ <full-name>%@</full-name> \ <score>%@</score> \ </high-score>", "Bobby", "10005", nil] dataUsingEncoding:NSASCIIStringEncoding];
Painting Barcode In Objective-C
Using Barcode creation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Draw UCC.EAN - 128 In Objective-C
Using Barcode printer for iPhone Control to generate, create UCC - 12 image in iPhone applications.
www.OnBarcode.com
CHAPTER 2: Responsive Social Gaming with RESTful Web Services
Generate QR Code ISO/IEC18004 In Objective-C
Using Barcode generation for iPhone Control to generate, create QR-Code image in iPhone applications.
www.OnBarcode.com
DataMatrix Creator In Objective-C
Using Barcode generator for iPhone Control to generate, create Data Matrix ECC200 image in iPhone applications.
www.OnBarcode.com
NSString *URLstr = HIGH_SCORES_URL; NSURL *theURL = [NSURL URLWithString:URLstr]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL]; [theRequest setHTTPMethod:@"POST"]; [theRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; [theRequest setHTTPBody: highScoreData]; [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (!theConnection) { // the connection request is invalid; malformed URL, perhaps }
Barcode Encoder In Objective-C
Using Barcode creation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Encode UPC-E In Objective-C
Using Barcode maker for iPhone Control to generate, create UPCE image in iPhone applications.
www.OnBarcode.com
In the code in Listing 2-20, first you put your data in an XML format similar to the high scores you parsed earlier. The difference is that because this is a new record, you can leave out the <id>, <created_at>, and <updated_at> tags just the full name and score are sufficient. In the listing, I broke up the string into several lines for readability, not because it must go on separate lines. After you create the XML data, you create an NSURL instance with your URL string, and then create an NSMutableURLRequest with it. Mutable means changeable, indicating you are going to change some settings in this request from their defaults. In this case, you are making the following changes: Change the HTTP method from GET to POST. Set the Content-type header to let the web service know what kind of data you are sending. Put the data in the request body. When you were getting the top-100 scores, you used an NSURLRequest, rather than an NSMutableURLRequest, because you didn t need to change the request from the default settings. After you create the request, you need to submit it to the simple high-score service. As with the previous top-100 high scores processing, you create an NSURLConnection and set your delegate, and then the delegate is called as different URL loading events take place. As you learned in the earlier discussion of RESTful web services, the service will respond with the
QR-Code Drawer In None
Using Barcode generator for Font Control to generate, create QR Code image in Font applications.
www.OnBarcode.com
QR Code Recognizer In Visual C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
CHAPTER 2: Responsive Social Gaming with RESTful Web Services
Scanning QR Code ISO/IEC18004 In C#
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Generate EAN 13 In None
Using Barcode generation for Font Control to generate, create European Article Number 13 image in Font applications.
www.OnBarcode.com
high-score record in XML format, with the <id>, <created_at>, and <updated_at> tags set appropriately. But you just want to know that your high-score submission worked! You don t generally need to examine your submitted data again with all the tags that the web service returns. You can simplify the connection delegate for submitting high scores by just checking for connection errors and whether the response status has a status code of 200, which means everything went OK. This is why in this example of creating an NSURLConnection, you only check if it is nil, and don t bother to create a data object to hold the data that is returned. You just want to know if there was an error connecting, if there was an error at the server, and the value of the response status code. Let s see how that works in Listing 2-21.
Barcode Scanner In Visual Studio .NET
Using Barcode Control SDK for ASP.NET Control to generate, create, read, scan barcode image in ASP.NET applications.
www.OnBarcode.com
Generating GTIN - 12 In Java
Using Barcode drawer for Java Control to generate, create UPC-A Supplement 2 image in Java applications.
www.OnBarcode.com
Listing 2-21. The Delegate Methods for Checking If High Score Submission Was Successful
Encode Code 3 Of 9 In .NET
Using Barcode generator for ASP.NET Control to generate, create Code 3 of 9 image in ASP.NET applications.
www.OnBarcode.com
Decoding Barcode In Visual Basic .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; // alert the user there has been an error submitting the high score NSLog(@"Error connecting - %@", [error localizedFailureReason]); [connection release]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSHTTPURLResponse *HTTPresponse = (NSHTTPURLResponse *)response; NSInteger statusCode = [HTTPresponse statusCode]; if ( 404 == statusCode || 500 == statusCode ) { [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; // alert the user there has been an error submitting the high score [connection cancel]; NSLog(@"Server Error - %@", [ NSHTTPURLResponse localizedStringForStatusCode:statusCode ]); } else if ( 200 == statusCode) { // the high score submission was successful!
Code 128 Code Set B Reader In Visual Studio .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Recognizing EAN / UCC - 13 In C#
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
CHAPTER 2: Responsive Social Gaming with RESTful Web Services
Painting Code-128 In VB.NET
Using Barcode generator for .NET framework Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications.
www.OnBarcode.com
EAN-13 Supplement 5 Creation In VS .NET
Using Barcode generator for Reporting Service Control to generate, create EAN / UCC - 13 image in Reporting Service applications.
www.OnBarcode.com
// do whatever other processing you need here [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE]; } }
If you wanted to have the web service modify a record, you could reuse the code from Listings 2-20 and 2-21. Instead of setting the HTTP method to POST, you would use the HTTP method PUT. As the URI, you would use the address of the representation of the record you want to modify, such as localhost:3000/high_scores/1.xml. Then you would just make sure that the record was modified properly by checking the response for status code 200. To delete a record, you would use the HTTP method DELETE, and use the URI for the specific record you want to delete. You could omit the Content-type header and the body, because you don t need to send any data to request a deletion. You just need to send the DELETE request to localhost:3000/high_scores/1.xml and check the response for status code 200.
Copyright © OnBarcode.com . All rights reserved.