2015-07-02 99 views
4

此JSON字符串有要發送:AFNetworking GET參數

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

使用GET以這種格式方法(因爲服務器檢索數據與REST API這個PHP代碼$_GET["setting"])與AFHTTPRequestOperationManager,使得對應的網址變爲:

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

當我創建一個在AFHTTPRequestOperationManagerGET:parameters:success:failure:參數它增加了網址關鍵參數到參數字典本身是這樣的:

{ 
    "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 
    }]; 
+0

爲什麼這不是POST或PUT?由於API使用GET方法,因此請在您創建的位置顯示代碼並設置字典和JSON – Wain

+0

@Wain。添加了代碼。我不知道這怎麼能實現。一些關於此的指針確實有幫助。 – Smarto

回答

5

試試這個:

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

如果你知道的話,請你能解釋它爲什麼會發生? –

0

你可以讓PHP開發者改變了GETrequest讓你在POST發送數據,使您的數據可以大量且安全地發送