我試圖使用AFNetworking 2.0從Parse.com後端獲取記錄。我想在某個日期後更新記錄。 Parse.com文件指出,對日期字段比較查詢需要進行網址的格式編碼:Parse.com和AFNetworking 2.0日期查詢
'where={"createdAt":{"$gte":{"__type":"Date","iso":"2011-08-21T18:02:52.249Z"}}}'
這工作完全使用curl。
在我的應用程序中,我使用AFNetworking 2.0運行查詢如下。我首先初始化共享客戶端時所設置的請求和響應串行:
+ (CSC_ParseClient *)sharedClient {
static CSC_ParseClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL *baseURL = [NSURL URLWithString:@"https://api.parse.com"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config setHTTPAdditionalHeaders:@{ @"Accept":@"application/json",
@"Content-type":@"application/json",
@"X-Parse-Application-Id":@"my app ID",
@"X-Parse-REST-API-Key":@"my api key"}];
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024
diskCapacity:50 * 1024 * 1024
diskPath:nil];
[config setURLCache:cache];
_sharedClient = [[CSC_ParseClient alloc] initWithBaseURL:baseURL
sessionConfiguration:config];
_sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
_sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
});
return _sharedClient;
}
- (NSURLSessionDataTask *)eventsForSalesMeetingID:(NSString *)meetingID sinceDate:(NSDate *)lastUpdate completion:(void (^)(NSArray *results, NSError *error))completion {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *dateString = [dateFormatter stringFromDate:lastUpdate];
NSLog(@"date = %@", dateString);
NSDictionary *params = @{@"where": @{@"updatedAt": @{@"$gte": @{@"__type":@"Date", @"iso": dateString}}}};
NSURLSessionDataTask *task = [self GET:@"/1/classes/SalesMeetingEvents"
parameters:params
success:^(NSURLSessionDataTask *task, id responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
NSLog(@"Response = %@", httpResponse);
if (httpResponse.statusCode == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(responseObject[@"results"], nil);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, nil);
});
NSLog(@"Received: %@", responseObject);
NSLog(@"Received HTTP %d", httpResponse.statusCode);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(nil, error);
});
}];
return task;
}
但是這會產生從服務器400的錯誤。該URL編碼的查詢字符串解碼後返回這個樣子的:
where[updatedAt][$gte][__type]=Date&where[updatedAt][$gte][iso]=2014-01-07T23:56:29.274Z
我試圖硬編碼的查詢字符串這樣的後端:
NSString *dateQueryString = [NSString stringWithFormat:@"{\"$gte\":{\"__type\":\"Date\",\"iso\":\"%@\"}}", dateString];
NSDictionary *params = @{@"where":@{@"updatedAt":dateQueryString}};
這讓我更接近,但仍然是一個400錯誤;從服務器返回的查詢字符串如下所示:
where[updatedAt]={"$gte":{"__type":"Date","iso":"2014-01-07T23:56:29.274Z"}}
如何從AFNetworking獲取正確的查詢字符串?我開始使用ParseSDK,這使得這個查詢變得非常簡單,但是他們的SDK是沉重的(30+ MB)。
是您的'requestSerializer'設置爲'AFJSONRequestSerializer'的實例? –
看起來它仍然被設置爲默認的「AFHTTPRequestSerializer」。 –
不,共享客戶端設置爲使用JSON序列化程序: – Alpinista