2015-04-29 90 views
0

我正在開發一個應用程序,因爲我曾經通過WebServices將我的聯繫號碼發送到服務器。在這裏,我得到了我的聯繫人號碼數組,但是如何將我的聯繫人數組發送到for- loop.Here服務鏈接。通過數組元素循環

http://project.in/project-contacts.php 

參數contact_numbers[]

這裏下面我試圖代碼

NSString *postVarArrayString = @""; 
    NSString *separator = @"?"; 
    for (int i=0; i<[_numberArray count]; i++) { 
     if (i>0) { 
      separator = @"&"; 
     } 
     postVarArrayString = [NSString stringWithFormat:@"%@%@myArray[]=%ld", postVarArrayString, separator, (long)[[_numberArray objectAtIndex:i] integerValue]]; 
    } 

    // url 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat: 
             @"http://project.in/project-contacts.php" 
             @"%@" 
             , postVarArrayString] 
        ]; 

    NSLog(@"qq=%@", [url absoluteString]); 

在這裏,我怎樣才能WebServices.Can送我的聯繫人的陣列請你建議我我如何發送,謝謝。

+4

將整個數組發送到服務器。讓服務器端腳本完成這項工作。 –

回答

0

發送字典作爲參數,以便您的PHP腳本抓取這些參數與$_POST變量。

使用示例AFNetworking庫。

NSArray *keys = [NSArray arrayWithObjects: @"key1", @"key2", nil]; 
NSArray *values = [NSArray arrayWithObjects: @"value1", @"value2", nil]; 
NSDictionary *parameters = [NSDictionary dictionaryWithObjects: values Keys: keys]; 

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL[NSURL URLWithString:@"Your server url"]; 
NSURLRequest *request = [client requestWithMethod:@"POST" path:@"path" parameters:parameters]; 

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
}]; 
[operation start];