2012-11-28 248 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])