I am using NSURLSession
and NSURLSessionDataTask
to send a post request and then get the response and store it in a variable that is an NSString and then log it. My function right now looks like this:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://mysite.org/mobile_app/studentregister.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params = [NSString stringWithFormat:@"username=%@&displayname=%@&password=%@&passwordc=%@&email=%@&teachercode=%@", username, displayname, password, passwordc, email, teacherCode];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Json = %@",text);
}
}];
[dataTask resume];
When I run the request it returns the following in the debugging terminal:
Json = {"userCharLimit":"Your username must be between 5 and 25 characters in length"}{"displaynameCharLim":"Your displayname must be between 5 and 25 characters in length"}{"passLimit":"Your password must be between 8 and 50 characters in length"}{"emailInvalid":"Not a valid email address"}{"teacherCodeLength":"Your teacher code is not 5 to 12 characters"}
How can I know parse this using NSJSONSerialization
? Can this even be parsed or do I have some formatting to do on the php? This is my first web service.