2012-11-28 344 views
0

我送JSON数据到服务器,并得到错误发送JSON数据到服务器

JSON -

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

错误 -

  • 服务器遇到错误处理请求。异常消息是'SqlDateTime溢出。必须介于1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间。在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]; 
+0

请显示如何形成日期值。我的习惯是使用nsdateformatter格式化为一个字符串,这很容易被我的服务器解析。 – danh

+0

[链接](http://stackoverflow.com/questions/6306718/nstimeinterval-format-for-post-with-json-objective-c-to-datetime-net-format) –

回答

1

答案是乘以1000,当您创建时间戳(在你提供的链接)。

1354119549233是周五,44880 5月03日23点13分53秒GMT

1354119549是星期三,2012 11月28日16点19分09秒GMT

+0

谢谢,但它没有解决问题。 –

+0

显示在服务器上处理请求的代码? – InsertWittyName

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你的要求,你已经拥有了它,%编码,设置内容长度等

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

the_date_param = DateTime.parse(params[:the_date_param])