我正在嘗試使用AFNetworking 3.0 POST請求。在xcode 8(Objective-c)中使用AFNetworking 3.0的POST請求?
到目前爲止,我沒有找到這個問題的確切答案。要麼我不明白,或者某些代碼已被棄用。
錯誤是「dataTaskWithRequest已經過時」
我有這個兩(2)文本框需要被後到Web服務器。
1.電子郵件
2. PW
到目前爲止,沒有工作。當前的代碼如下
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize email,pw;
- (IBAction)sendData:(id)sender {
NSString *URLString = @"http://localhost/test.php";
NSDictionary *parameters [email protected]{@"email" : @"pw"};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:nil error:nil];
req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"email"] longValue];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Accept"];
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSLog(@"Reply JSON: %@", responseObject);
if ([responseObject isKindOfClass:[NSArray class]]) {
NSLog(@"Response == %@",responseObject);
}
} else {
NSLog(@"Error: %@, %@, %@", error, response, responseObject);
}
}]resume];
}
@end
這是什麼?:''req.timeoutInterval = [[[NSUserDefaults standardUserDefaults] valueForKey:@「email」] longValue];' –
請看看[this]的接受的答案(http://stackoverflow.com/questions/ 34561215/afnetworking-3-0-遷移如何對後具有報頭和-HTTP-體)。 – Gour