Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将cURL请求(使用--data urlencode)转换为afget请求_Ios_Nsurlrequest_Afnetworking - Fatal编程技术网

Ios 将cURL请求(使用--data urlencode)转换为afget请求

Ios 将cURL请求(使用--data urlencode)转换为afget请求,ios,nsurlrequest,afnetworking,Ios,Nsurlrequest,Afnetworking,这个问题可能与AFN网络无关,但更多的是在构造NSURLRequest时。 我正在尝试使用AFNetworking发出GET请求- curl -X GET \ -H "X-Parse-Application-Id: Q82knolRSmsGKKNK13WCvISIReVVoR3yFP3qTF1J" \ -H "X-Parse-REST-API-Key: iHiN4Hlw835d7aig6vtcTNhPOkNyJpjpvAL2aSoL" \ -G \ --data-urlencod

这个问题可能与AFN网络无关,但更多的是在构造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

然而,我不知道如何写

[AFHTTPClient getPath:参数:成功:失败:]


对于这个请求。where子句看起来不像字典,但是此函数仅将字典作为其参数输入。

该参数需要一个
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);
        }];

第九天我来了!