为什么我从AFN服务器得到空数组?

时间:2016-12-21 13:25:27

标签: ios objective-c afnetworking

我称这种方法为:

-(void)getdocschedule{

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *t = [prefs stringForKey:@"token"];
    NSString *seqid= [prefs stringForKey:@"seq_id"];
    NSLog(@"%@",t);
    NSLog(@"%@",seqid);

    AppDelegate *del = [[UIApplication sharedApplication] delegate];

    NSString *Loginurl = [NSString stringWithFormat:@"%@/GetSchedule",del.Root_URL];


    NSDictionary *params = @{
                             @"id":seqid,

                             @"token":t

                             };

    //here we can see parameters which is sent to server

    NSLog(@"Sent parameter to image server  : %@",params);

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

    [policy setValidatesDomainName:NO];

    [policy setAllowInvalidCertificates:YES];

    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.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];

         [manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {

        // Here we can see response which is coming from server

        NSLog(@"Response from get schedule server:%@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);

我得到的反应如下:

Response from get schedule server:[]

但为什么我的回复为null?我不明白,我错了?

1 个答案:

答案 0 :(得分:0)

试试这段代码。我认为问题在于您使用AFHTTPSessionManager的旧折旧方法。根据您的要求更改URL和参数,然后运行。

P.S。尝试在POSTMAN中测试您的API,并检查您的API是否返回了一些数据,或者没有使用params。

-(void)getdocschedule{

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *t = [prefs stringForKey:@"token"];
NSString *seqid= [prefs stringForKey:@"seq_id"];
NSLog(@"%@",t);
NSLog(@"%@",seqid);

NSString *Loginurl = [NSString stringWithFormat:@"%@%@",kAPIBaseURL,kLoginAPI];


NSDictionary *params = @{
                         @"email":@"a@a.com",

                         @"password":@"Hello@1212"

                         };

//here we can see parameters which is sent to server

NSLog(@"Sent parameter to image server  : %@",params);

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

AFSecurityPolicy* policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];

[policy setValidatesDomainName:NO];

[policy setAllowInvalidCertificates:YES];

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.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];
[manager POST:Loginurl parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) {

}failure:^(NSURLSessionTask *task, NSError *error) {


      }];
}