2016-03-07 82 views
1

我想嘗試在以下API中發佈參數,但參數沒有正確傳遞,並且收到的響應給出了消息,即數據是必需的。所以任何人都可以請幫忙解決這個問題。AFNetworking 2.0用字典數組發送帖子請求參數

我的網址是

forapp.com/api/getContacts.php?data= [ { 「名」: 「ABC」, 「手機」: 「1234567890」},{ 「名」: 「凱特 鍾」, 「手機」: 「9925992599」}]

,所以我怎麼能這樣類型的請求傳遞給API

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    NSDictionary *params = @{@"name": @"hello", 
          @"phone": @"1234567890"}; 
    NSLog(@"Dict %@",params); 
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 

}]; 
+1

通常這樣。它非常容易調用與AFNetworking 2.0 WS http://stackoverflow.com/questions/33767908/volunteermatch-api-objective-c/33886449#33886449 – Vvk

+0

看看我的答案希望這有助於: - http://stackoverflow.com/a/35497323/3515115 –

回答

6

只需添加

步驟-1

// create the dictionary of {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} this 

NSDictionary *params = @{@"name": @"hello", 
         @"phone": @"1234567890"}; 

步驟2

//create the one array of this output [ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ] 

NSMutableArray *arr = [NSMutableArray arrayWithObjects:params,nil]; 

步驟3

// convert your object to JSOn String 
NSError *error = nil; 
NSString *createJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart 
                       options:NSJSONWritingPrettyPrinted 
                        error:&error] 
             encoding:NSUTF8StringEncoding]; 

步驟4

步驟-5

// finally start your request 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

    NSLog(@"Dict %@",pardsams); 
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:pardsams success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 

}]; 
+0

所以我必須在參數傳遞數組? – Birendra

+0

我不知道你可以傳遞數組給你的請求,我們不知道你的請求的原因,嘗試一次 –

+0

不能正常工作它的給出相同的響應數據 – Birendra

1

按照這個網址

forapp.com/api/getContacts.php?data=[ {"name":"abc","phone":"1234567890"},{"name":"Kate Bell","phone":"9925992599"} ] 

你應該實現這樣

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    NSDictionary *params = @{@"name": @"hello", 
          @"phone": @"1234567890"}; 
    NSLog(@"Dict %@",params); 
manager.responseSerializer = [AFJSONResponseSerializer serializer]; 

manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"]; 

[manager POST:@"http://forapp.com/api/getContacts.php" parameters:@{@"data": @[params]} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 

}]; 
1

添加以下內容:

// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager 
httpClient.requestSerializer = AFJSONRequestSerializer; 

而且還檢查你的發佈數組不包含任何其他對象。它只包含數組。