2016-08-21 80 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將其自動轉換爲整數。所以服務器正在獲取整數值,並且無法投射此值。我已經在服務器上進行了更改以解決此錯誤。

相關問題