2016-09-20 300 views
0

我當時是使用AFNETWORKING 3.0以下參數到服務器的請求:AFNETWORKING 3.0 POST請求失敗

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=Request failed: not found (404), NSUnderlyingError=0x7be19ef0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: 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"; "Content-Length" = 302; "Content-Type" = "text/html; charset=iso-8859-1"; Date = "Tue, 20 Sep 2016 08:12:20 GMT"; "Keep-Alive" = "timeout=5, max=100"; Server = "Apache/2.4.7 (Ubuntu)"; } }, [email protected]"mylink" , NSLocalizedDescription=Request failed: unacceptable content-type: text/html,

請幫我解決這個錯誤。

+0

你應該使用下面的方法,這是我的代碼我使用........ – sohail

+0

這是我的參數我想發送 – sohail

+0

這個代碼即時通訊使用 – sohail

回答

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); 
    }]; 

希望這個事情的作品