【发布时间】:2017-01-09 03:53:01
【问题描述】:
我正在尝试使用 AFNetworking 3.0 发布请求。
到目前为止,我还没有找到这个问题的确切答案。要么我不明白,要么某些代码已被弃用。
错误是 “dataTaskWithRequest 已弃用”
我有这 两 (2) 个需要发布到网络服务器的文本字段。
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" : @"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接受的答案。
标签: objective-c xcode8 afnetworking-3