我使用AFNETWORKING 3.0
向服务器发送了一个请求,其中包含以下参数:
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:_texLoginUname.text forKey:@"ent_email"];
[parameters setObject:_textLoginPassword.text forKey:@"ent_password"];
[parameters setObject:uniqueIdentifier forKey:@"ent_device_id"];
[parameters setObject:@"2" forKey:@"eat_device_type"];
[parameters setObject:@"22" forKey:@"ent_device_token"];
这是我的代码:
NSString *URL = @"http://optime.in/apps/food_truck/user_api/login";
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:URL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSLog(@"success!");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: errorData options:kNilOptions error:nil];
NSLog(@"val = %@",serializedData);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
message:@"Sorry, try again."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
我收到了这个错误:
NSLocalizedDescription =请求失败:未找到(404), NSUnderlyingError = 0x7be19ef0 {错误域= com.alamofire.error.serialization.response代码= -1016"请求失败:不可接受的内容类型:text / html"
UserInfo = {com.alamofire.serialization.response.error.response = http://optime.in/apps/food_truck/user_api/login} {status code:404,headers { Connection =" Keep-Alive&#34 ;; " Content-Length的" = 302; "内容类型" =" text / html;字符集= ISO-8859-1&#34 ;; 日期="星期二,2016年9月20日08:12:20 GMT&#34 ;; "保活" ="超时= 5,最大= 100&#34 ;; Server =" Apache / 2.4.7(Ubuntu)&#34 ;; ,NSErrorFailingURLKey = @" mylink" ,NSLocalizedDescription =请求失败:不可接受的内容类型:text / html,
请帮我解决这个错误。
答案 0 :(得分:2)
你应该使用以下方法,因为AFJSONRequestSerializer
不起作用是AFNetworking 3.0
这个方法可以工作,因为我只使用这个方法
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:_texLoginUname.text forKey:@"ent_email"];
[parameters setObject:_textLoginPassword.text forKey:@"ent_password"];
[parameters setObject:uniqueIdentifier forKey:@"ent_device_id"];
[parameters setObject:@"2" forKey:@"eat_device_type"];
[parameters setObject:@"22" forKey:@"ent_device_token"];
NSString *URL = @"http://optime.in/apps/food_truck/user_api/login";
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager POST:URL parameters:_parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success!");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error: %@", error);
}];
希望这件事有效