Web服务在iOS中返回空数据

时间:2017-11-23 17:55:52

标签: ios objective-c web-services

状态代码为200,但不返回数据。当我获得状态时,数据打印的输出是

OS_dispatch_data:data [0x60000046b080] = {leaf,size = 53,buf = 0x600000674580}

当数据输出并且json对象被反序列化时,它显示以下数据字典

{
    message = "Some input field missing";
    status = 406;
}

这是我的代码

NSError *error;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"xxx"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:120.0];

    NSMutableDictionary * mapData=[NSMutableDictionary new];

        [mapData setObject: [Crs_sharedvariable sharedMySingleton].crs_country forKey:@"country_name"];

    [mapData setObject:[Crs_sharedvariable sharedMySingleton].crs_userId forKey:@"user_id"];

    NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:NSJSONWritingPrettyPrinted error:&error];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSLog(@"%@",[[request URL]absoluteString]);
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        dispatch_async(dispatch_get_main_queue(), ^{

            NSLog(@"error %@",error);
            NSLog(@"data %@",data);
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONWritingPrettyPrinted error:nil];
            NSLog(@"%@", json );
            NSLog(@"%@", [json valueForKey:@"message"]);

        });

    }];

0 个答案:

没有答案