NSURLSession
を使用してポストリクエストを作成しようとしています。私は二重の値を含むデータを送る必要があります。私はNSNumber
を使ってdouble値をNSDictionary
の中に格納しました。私はこのようなデータを掲載しています:私はNSLog
を使用してのparams印刷する場合サーバーの応答 "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];
}
、それはこのように印刷されます。このエラーは、明らかである
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;
}
:よう
params : {
budget = 1000;
distance = 50;
latitude = "30.73979949951172";
longitude = "76.78269958496094";
pageNo = 0;
rating = 4;
}
Serverが応答を返していますサーバが倍精度整数値を列挙できないことを示しています。距離パラメータのためにこのエラーが発生しています。私は距離を二重の値として渡していますが、サーバがそれを整数値としてどのように見つけていますか?
問題は、これらのパラメータがサーバーによって認識されないことです。 Google ChromeのSwagger UIを使用してPOSTを送信したため、サーバーの部分が正しく機能し、正常に機能しました。
あなたの質問は、投稿要求を行うための実際のコードで更新してください。あなたの質問を、投稿要求をするときに持っている実際の問題に更新してください。 – rmaddy
コードを更新しました。確認してください。まだ不明な点がある場合は、私に知らせてください。 – Rains
あなたはサーバーコードが間違っています。 – Avi