[英]sending json data to server
我正在將json數據發送到服務器並出現錯誤
json-
錯誤-
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];
答案是創建時間戳時不要乘以1000(如您提供的鏈接中所示)。
1354119549233是星期五,格林尼治標准時間44880年5月3日23:13:53
1354119549是2012年11月28日,星期三,格林尼治標准時間
這是我將日期傳遞到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添加到您的請求中,如已獲取,%編碼,設置內容長度等。
服務器端需要將日期解析為日期。 在rails中,使用上面的格式字符串,將導致如下所示的日期解析...
the_date_param = DateTime.parse(params[:the_date_param])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.