簡體   English   中英

遍歷數組元素

[英]Loop through array elements

我正在開發一個應用程序,因為我曾經通過WebServices將聯系方式發送到服務器。在這里,我獲得的聯系方式是一個數組,但是我如何在for-loop上發送我的聯系方式。這里是服務鏈接。

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發送聯系人數組。能否請您建議我如何發送,謝謝。

發送字典作為參數,以便您的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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM