此问题可能与AFNetworking无关,但在构建NSURLRequest时更多。 我正在尝试使用AFNetworking-
发出下降的GET请求curl -X GET \
-H "X-Parse-Application-Id: Q82knolRSmsGKKNK13WCvISIReVVoR3yFP3qTF1J" \
-H "X-Parse-REST-API-Key: iHiN4Hlw835d7aig6vtcTNhPOkNyJpjpvAL2aSoL" \
-G \
--data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/GameScore
这是来自parse.com API https://parse.com/docs/rest#queries-constraints。
但是,我无法弄清楚如何写
[AFHTTPClient getPath:parameters:success:failure:]
此请求。 where子句看起来不像字典,但是这个函数只为其参数输入获取字典。
答案 0 :(得分:6)
参数需要NSDictionary
,它将在URL中转换为键/值对。因此关键很容易,但在将其设置为字典之前,您需要将其转换为JSON ...
NSDictionary *jsonDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Sean Plott", @"playerName",
[NSNumber numberWithBool:NO], @"cheatMode", nil];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
if (!jsonData) {
NSLog(@"NSJSONSerialization failed %@", error);
}
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:
json, @"where", nil];
如果我们假设你的客户端配置了这样的东西(通常你是子类AFHTTPClient
并且可以移动这些东西
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.parse.com/"]];
[client setDefaultHeader:@"X-Parse-Application-Id" value:@"Q82knolRSmsGKKNK13WCvISIReVVoR3yFP3qTF1J"];
[client setDefaultHeader:@"X-Parse-REST-API-Key" value:@"iHiN4Hlw835d7aig6vtcTNhPOkNyJpjpvAL2aSoL"];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
然后你应该可以打电话了
[client getPath:@"1/classes/GameScore"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed %@", error);
}];