Aby wysłać dane rejestracyjne do serwera, Używam JSON jest w następującej formie:Wyślij zagnieżdżony JSON za pomocą AFNetworking
Oto jak wysyłam.
NSURL * url = [[NSURL alloc] initWithString:registerUrlString];
AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
NSDictionary * params = @{@"regData": @{
@"City": self.cityField.text,
@"Country": self.countryField.text,
@"Email_Id": self.emailField.text,
@"MobileNumber": self.numberField.text,
@"UserName": self.userName.text,
}
};
NSMutableURLRequest * request = [httpClient requestWithMethod:@"POST" path:registerUrlString parameters:params];
AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success: %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Error: %@", [error debugDescription]);
}];
[operation start];
Ale niestety ja otrzymuję ten błąd:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x94b3c30 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Twoja metoda nie jest generyczna. Sprawdź właściwą metodę [tutaj] (http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class). Mniej podatny na błędy i konserwowalny –