繁体   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