使用一个 NSSet 作为值发布 NSDictionary 的 HTTPRequest

发布于 2024-12-19 18:09:40 字数 1937 浏览 1 评论 0原文

我有一个具有一对多关系的核心数据对象。例如

person.mothername
人.父亲姓名
person.childrennames <- 这是一个 NSSet

现在我想通过一个 HTTP 请求立即将其从 iPhone 发布到我们的服务器。这可以做到吗?

我现在使用的代码是一个没有 NSSet 的简单示例: 设置参数:

params = [NSDictionary dictionaryWithObjectsAndKeys:
              self.person.mothername, @"mothername",
              self.person.fathername, @"fathername",
              nil];
self.httpRequest = [[HTTPRequest alloc] init];
self.httpRequest.delegate = self;
[self.httpRequest httpPOST:SERVER_POST_PERSON withParams:params];

然后下一步(=简单):

- (BOOL)httpPOST:(NSString *)url withParams:(NSDictionary *)params {

const char *strUrlConst = (const char*)[[self buildParams:params] UTF8String]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[NSData dataWithBytes:strUrlConst length:strlen(strUrlConst)]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    self.responseData = [[NSMutableData data] retain];
    return YES;
}

return NO;

}

然后最后的方法(简单?):

- (NSString *)buildParams:(NSDictionary *)params {
NSString *builtParams = [[[NSString alloc] init] autorelease];

int i = 0;
for (NSString *key in [params allKeys]) {
    NSString *value = [self escapeString:[params objectForKey:key]];
    if (!value) {
        continue;
    }
    if (!i++) {
        builtParams = [builtParams stringByAppendingString:[NSString stringWithFormat:@"%@=%@", key, value]];
    } else {
        builtParams = [builtParams stringByAppendingString:[NSString stringWithFormat:@"&%@=%@", key, value]];
    }
}

NSLog(@"PARAMS: %@", builtParams);
return builtParams;

}

I have a core data object with a one-to-may relation. So for instance

person.mothername
person.fathername
person.childrennames <- this is a NSSet

Now I want to post this at once from the iPhone to our server through one HTTP request. Can this be done?

The code that I use right now for a plain example without NSSet:
Setting the parameters:

params = [NSDictionary dictionaryWithObjectsAndKeys:
              self.person.mothername, @"mothername",
              self.person.fathername, @"fathername",
              nil];
self.httpRequest = [[HTTPRequest alloc] init];
self.httpRequest.delegate = self;
[self.httpRequest httpPOST:SERVER_POST_PERSON withParams:params];

Then the next step (= straightforward):

- (BOOL)httpPOST:(NSString *)url withParams:(NSDictionary *)params {

const char *strUrlConst = (const char*)[[self buildParams:params] UTF8String]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[NSData dataWithBytes:strUrlConst length:strlen(strUrlConst)]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    self.responseData = [[NSMutableData data] retain];
    return YES;
}

return NO;

}

Then the final method (straightforward?):

- (NSString *)buildParams:(NSDictionary *)params {
NSString *builtParams = [[[NSString alloc] init] autorelease];

int i = 0;
for (NSString *key in [params allKeys]) {
    NSString *value = [self escapeString:[params objectForKey:key]];
    if (!value) {
        continue;
    }
    if (!i++) {
        builtParams = [builtParams stringByAppendingString:[NSString stringWithFormat:@"%@=%@", key, value]];
    } else {
        builtParams = [builtParams stringByAppendingString:[NSString stringWithFormat:@"&%@=%@", key, value]];
    }
}

NSLog(@"PARAMS: %@", builtParams);
return builtParams;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-12-26 18:09:40

您可以使用 NSSet-allObjects+setWithArray: 将其转换为 NSArray 并返回。

You can use NSSet's -allObjects and +setWithArray: to convert it to an NSArray and back.

做个少女永远怀春 2024-12-26 18:09:40

为什么不使用 JSON?
像 SBJson 这样的库可以用一行代码从 NSDictionary 创建 JSON 字符串(IIRC:[params JSONRepresentation])。使用 NSString 的百分比 esacpes 方法对其进行 URL 编码非常简单。
在服务器端应该有大量的 JSON 库可供选择来解析数据。

Why not use JSON?
Libraries like SBJson can create a JSON string from an NSDictionary in one line of code (IIRC: [params JSONRepresentation]). It is straightforward to URL encode it by using NSString's percent esacpes method.
On the server side there should be plenty of JSON libraries to choose from for parsing the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文