0

1対5の関係を持つコアデータオブジェクトがあります。だから例えば

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

これをiPhoneからサーバーに1つのHTTPリクエストで一度に投稿したいと思います。これはできますか?

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;

}

4

2 に答える 2

0

JSON を使用しない理由
SBJson のようなライブラリは、1 行のコードで NSDictionary から JSON 文字列を作成できます (IIRC: [params JSONRepresentation])。NSString のパーセント esacpes メソッドを使用して URL エンコードするのは簡単です。
サーバー側では、データを解析するために選択できる JSON ライブラリがたくさんあるはずです。

于 2011-12-03T09:39:28.620 に答える
0

NSSet-allObjectsand+setWithArray:を使用して、 and に変換できますNSArray

于 2011-12-03T11:49:38.993 に答える