AFNetworking GET parameters with JSON (NSDictionary) string contained in URL key parameter

烈酒焚心 提交于 2019-12-24 16:46:24

问题


This JSON string has to be sent:

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

to a REST API using GET method in this format (since server retrieves data with this PHP code $_GET["setting"]) with AFHTTPRequestOperationManager, such that the equivalent URL becomes:

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

When I create an NSDictionary of parameters in AFHTTPRequestOperationManager's GET:parameters:success:failure: which adds the url key parameter to the parameter dictionary itself like this:

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

In short only the JSON string must be encapsulated in setting parameter NOT as object of setting in a JSON string.

Edit: Here's the code:

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
     }];

回答1:


Try this:

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
     }];



回答2:


you can ask the php developer to change the GET To request so that you send data in POST so that your data can be sent in large amount and securely also



来源:https://stackoverflow.com/questions/31177080/afnetworking-get-parameters-with-json-nsdictionary-string-contained-in-url-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!