@Scholle,
1. NSURLRequest:, NSURLRequest NSURLConnetionDelegate. JSON.
NSUrlConnectionDidReceiveData
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData options:kNilOptions error:&error];
if (error != nil) {
NSLog(@"Error parsing JSON.");
}
else {
NSLog(@"Array: %@", jsonArray);
}
2. AFNetworking afnetworking 3.0 . , .
- (void)requestWithURL:(NSString *)url parameterDictionary:(NSMutableDictionary *)data requestType:(NSString *)reqType handler:(NSObject *)handler selector:(SEL)selector{
NSError *error;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:reqType URLString:[NSString stringWithFormat:@"%@",url] parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
[req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
if (data != nil) {
NSLog(@"Data is not nil");
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
}else{
NSLog(@"Data is nil");
}
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
[handler performSelector:selector withObject:responseObject];
if ([responseObject isKindOfClass:[NSDictionary class]]) {
}
} else {
NSLog(@"Error: %@, %@, %@", error, response, responseObject);
[handler performSelector:selector withObject:responseObject];
}
}] resume];
}