AFNetworking GET参数与JSON(NSDictionary中)字符串包含在URL关键参

2019-10-23 15:11发布

这JSON字符串必须发送:

{
"dashboard": "compact",
"theme": "dark",
"show_side_bar": "yes"
}

使用这种格式GET方法(因为服务器与此PHP代码检索数据的REST API $_GET["setting"]AFHTTPRequestOperationManager ,使得等效URL变为:

http://www.examplesite.com/api/change_setting?setting={ "dashboard" : "compact", "theme" : "dark", "show_side_bar" : "yes" }

当我创建NSDictionary的参数AFHTTPRequestOperationManagerGET:parameters:success:failure:这更增加了参数字典本身这样的URL关键参数 :

{
  "setting": {
    "dashboard": "compact",
    "theme": "dark",
    "show_side_bar": "yes"
  }
}

总之 JSON字符串必须在设置参数象在一个JSON字符串设置的对象进行封装。

编辑:下面的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{
                             kSettingDashboard: @"compact",
                             kSettingTheme: @"dark",
                             kSettingShowSideBar: @"yes"
                             };

[manager GET:kURLChangeSetting
  parameters:[NSDictionary dictionaryWithObject:parameters forKey:@"setting"]
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         // code
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         /// code
     }];

Answer 1:

尝试这个:

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
NSString *parametersString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[manager GET:kURLChangeSetting
  parameters:@{@"setting" : parametersString}
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         // code
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         /// code
     }];


Answer 2:

你可以让PHP开发人员改变GETrequest让你在发送数据的POST ,使您的数据可以大量发送,并且还安全



文章来源: AFNetworking GET parameters with JSON (NSDictionary) string contained in URL key parameter