2016-08-21 78 views
-1

我试图使用NSURLSession提出请求。我需要发送包含双值的数据。我使用NSNumberNSDictionary中存储双值。我张贴的数据是这样的:服务器响应“java.lang.Integer不能转换为java.lang.Double”

-(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,它打印出这样的:

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值传递,然后服务器如何将其作为整数值进行查找?

问题是服务器无法识别这些参数。服务器部分正常工作,因为我已经使用谷歌浏览器的Swagger UI发送了一个POST,并且它完美地工作。

+1

用您的实际代码更新您的问题以提出发布请求。用提出请求时遇到的实际问题更新您的问题。 – rmaddy

+0

我已经更新了代码,请检查它。如果它仍然不清楚,那么让我知道.. – Rains

+1

你有错误的服务器代码。 – Avi

回答

0

我得到这个错误,因为我发送50.0作为double值。目标C将其自动转换为整数。所以服务器正在获取整数值,并且无法投射此值。我已经在服务器上进行了更改以解决此错误。

相关问题