如何使用AFNetworking 3.0将POST
数据发送到服务器?
我尝试了以下方式,但出了点问题。
NSString *Loginurl = [NSString stringWithFormat:@"https://my_url"];
NSDictionary *params = @{@"mobile":self.phonenumber.text,
@"archive":@"schooldb1",
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",nil];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"Response from server 1 : %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
[self getdata:responseObject];
}
failure:^(NSURLSessionTask *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}
请给我关于这个问题的建议。
答案 0 :(得分:1)
查看此示例可能会对您有所帮助: -
NSString *bURL = @"http://your-url.com/";
NSString *path = @"method/url/";
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[parameters setObject:@"yourDictValue" forKey:@"YourDictkey"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:bURL];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
//here is place for code executed in success case
} failure:^(NSURLSessionDataTask *task, NSError *error) {
UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
message:@"Sorry, try again."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertV show];
NSLog(@"Err: %@", [error localizedDescription]);
}];
了解更多信息,请参阅此question