使用一个 NSSet 作为值发布 NSDictionary 的 HTTPRequest
我有一个具有一对多关系的核心数据对象。例如
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
NSSet
的-allObjects
和+setWithArray:
将其转换为NSArray
并返回。You can use
NSSet
's-allObjects
and+setWithArray:
to convert it to anNSArray
and back.为什么不使用 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.