Created
July 16, 2013 13:00
-
-
Save TorbenKoehn/6008460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ACTION}} = Die Aktion, die die PHP API empfangen soll | |
{{METHOD}} = Die HTTP Request Method (Meist GET für lesen, POST für schreiben) | |
{{GET_DATEN}} = Die GET Daten im Query String Format | |
{{POST_DATEN}} = Die POST Daten im Query String Format | |
//prepar request | |
NSString *urlString = [NSString stringWithFormat:@"http://deineurl.com/api.php?action={{ACTION}}&{{GET_DATEN}}"]; | |
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; | |
[request setURL:[NSURL URLWithString:urlString]]; | |
[request setHTTPMethod:@"{{METHOD}}"]; | |
//set headers | |
NSString *contentType = [NSString stringWithFormat:@"text/html"]; | |
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; | |
//create the body | |
NSMutableData *postBody = [NSMutableData data]; | |
[postBody appendData:[[NSString stringWithFormat:@"{{POST_DATEN}}"] dataUsingEncoding:NSUTF8StringEncoding]]; | |
//post | |
[request setHTTPBody:postBody]; | |
//get response | |
NSHTTPURLResponse* urlResponse = nil; | |
NSError *error = [[NSError alloc] init]; | |
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; | |
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; | |
NSLog(@"Response Code: %d", [urlResponse statusCode]); | |
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) { | |
NSLog(@"Response: %@", result); | |
//"result" ist nun der JSON String. Parsen und verarbeiten. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment