0

我正在向服务器发送 json 数据并出现错误

json -

  • { GETDatetime = "/日期(1354119549)/"; }

错误 -

  • 服务器在处理请求时遇到错误。异常消息是“SqlDateTime 溢出。必须介于 1753 年 1 月 1 日上午 12:00:00 和 9999 年 12 月 31 日晚上 11:59:59 之间。

obj-c 中的代码

NSString* json = [dict JSONRepresentation];
//NSString *myRequestString = @"param="; // Attention HERE!!!!
//myRequestString = [myRequestString stringByAppendingString:json];
NSURL *url = [NSURL URLWithString:reqUrlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [NSData dataWithBytes:[json UTF8String] length:[json length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
4

2 回答 2

1

答案是在创建时间戳时不要乘以 1000(如您提供的链接中所示)。

1354119549233 是格林威治标准时间 44880 年 5 月 3 日星期五 23:13:53

1354119549 是格林威治标准时间 2012 年 11 月 28 日星期三 16:19:09

于 2012-11-28T17:15:03.603 回答
0

以下是我将日期传递给(以及相反地,从)Web 服务的方式......

NSDate *someDate = [NSDate date];

// get the serverDateFormat in an initial transaction with the server
// or, simpler and more brittle, hardcode something like the following ...
NSString *serverDateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'";

// prepare a date formatter.  i allocate and hang onto this at init, but
// you can just do this inline to get started...
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:serverDateFormat];
[_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

// this is in a category method on NSDate called asHttpParam, but the code
// here works fine to get started. the following gets the date ready to pass to server...
NSString *dateParam = [_dateFormatter stringFromDate:someDate];

您提到的 SO 日期解决方案有几个问题,包括日期的纪元基础不明确,以及对本地调整的硬编码 GMT。相比之下,无论客户端的时区如何,dateParam 都是一个明确的绝对日期。

将 dateParam 添加到您的请求中,因为您已经拥有它、%-encoding、设置内容长度等。

服务器端需要将日期解析为日期。在 Rails 中,使用上面的格式字符串会导致日期解析看起来像这样......

the_date_param = DateTime.parse(params[:the_date_param])
于 2012-11-28T18:10:03.743 回答