繁体   English   中英

AFNetworking-AFHTTPRequestOperationManager不返回数据

[英]AFNetworking - AFHTTPRequestOperationManager does not return data

我正在使用AFHTTPRequestOperationManager进行登录。

- (IBAction)loginAction
{
    [TGProjectHandler clearCookies];

    NSDictionary *params = @{@"Email": _email.text,
                             @"Password": _password.text,
                             @"IsRemember": @"true",
                             @"ReturnUrl": @"",
                             @"UserId": @0,
                             @"OutResponse": @0};

    [_sharedHandler.requestManager POST:TGURL_LOGIN
                             parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

     NSError *e;
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&e];                                    
      NSLog(@"%@\n\n%@", jsonDict, responseObject);
      NSString *outResponse = responseObject[@"Object"][@"OutResponse"];
      if (outResponse.integerValue == 1){
         NSLog(@"login successful: %@", outResponse);
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"User_Logged_In"];
         [TGProjectHandler saveCookiesToDefaults];
         [self performSegueWithIdentifier:@"HomePage" sender:self];
       }else
        {
           [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:@"Invalid credentials" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
        }
     }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [TGProjectHandler dismissHUD];
         [[[UIAlertView alloc] initWithTitle:@"Login Failed" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
       }];
}

服务函数返回UserID,但这是我在NSLog得到的响应。

NSLog(@"%@\n\n%@", jsonDict, responseObject); 



Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}

{
    Object =     {
        OutResponse = 1;
        ReturnUrl = "<null>";
    };
    Success = 1;
}  

为什么UserId没有响应?

通过查看您的代码,我想问题可能出在您的请求中。 检查您的内容类型设置是否正确。

例如 -

[_sharedHandler.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

您也可以将响应序列化器更改为AFJSONResponseSerializer,它将自动将您的响应转换为字典或数组。

_sharedHandler.responseSerializer = [AFJSONResponseSerializer serializer];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM