服务器响应" java.lang.Integer无法强制转换为java.lang.Double"

时间:2016-08-21 05:56:22

标签: objective-c nsurlsession

我尝试使用NSURLSession发帖请求。我需要发送包含双值的数据。我使用NSNumberNSDictionary内存储了双值。我发布的数据如下:

 -(void)sortJobsWithLat:(float)lat longt:(float)longt distance:(float)distance budget:(NSInteger)budget rating:(NSInteger)rating page:(NSUInteger)page {

    NSError *error;
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURLJobs,SearchJobs]];
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
    NSLog(@"url : %@", url);

      NSDictionary *params=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithDouble:lat] ,@"latitude",[NSNumber numberWithDouble:longt],@"longitude", [NSNumber numberWithDouble:distance],@"distance",   [NSString stringWithFormat:@"%ld",(long)budget] ,@"budget", [NSString stringWithFormat:@"%ld",(long)rating],@"rating", [NSString stringWithFormat:@"%ld",page],@"pageNo",nil];

    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:params options:kNilOptions error:&error];


    NSLog(@"params : %@",params);

    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:jsonData];

    NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
    [urlRequest setHTTPBody:postData];

    dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                    if(error == nil)
                                    {
                                        NSError *jsonError;
                                        NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                                                                   options:kNilOptions
                                                                                                     error:&jsonError];
                                        NSLog(@"json object : %@",jsonObject);
                                    }
                                    else{
                                        [AppDel showAlertWithMessage:error.localizedDescription andTitle:nil];
                                    }
                                }];
    [dataTask resume];
}

当我使用NSLog打印参数时,它打印如下:

    params : {
    budget = 1000;
    distance = 50;
    latitude = "30.73979949951172";
    longitude = "76.78269958496094";
    pageNo = 0;
    rating = 4;
}

服务器返回的响应如下:

json object : {
    error = "Internal Server Error";
    exception = "java.lang.ClassCastException";
    message = "java.lang.Integer cannot be cast to java.lang.Double";
    path = "/jobs/searchJobs";
    status = 500;
    timestamp = 1471760398842;
}

此错误清楚地表明服务器无法将整数值设置为double。我因距离参数而导致此错误。我将距离作为double值传递给服务器如何将其作为整数值传递?

问题是服务器无法识别这些参数。服务器部分正常运行,因为我已经使用Swagger UI为Google Chrome发送了一个POST,但它运行良好。

1 个答案:

答案 0 :(得分:0)

我收到此错误是因为我将50.0作为双倍值发送。 Objective C自动将其转换为整数。所以服务器获得整数值,并且无法转换此值。我在服务器上进行了更改以解决此错误..