JSON Serialization を使用して json 応答を取得しました。ここでは問題なく取得できますが、いくつかの値を URL とのキーと値のペアとして投稿する必要がある場合。私はこのようにしましたが、結果は得られませんでした。
NSArray *objects = [NSArray arrayWithObjects:@"uname", @"pwd", @"req",nil];
NSArray *keys = [NSArray arrayWithObjects:@"ann", @"ann", @"login", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:keys forKeys:objects];
if ([NSJSONSerialization isValidJSONObject:dict]) {
NSError *error;
result = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error == nil && result != nil) {
// NSLog(@"Success");
}
}
NSURL * url =[NSURL URLWithString:@"URL_address_VALUE/index.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[result length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:result];
NSURLResponse *res = nil;
NSError *error = nil;
NSData *ans = [NSURLConnection sendSynchronousRequest:request returningResponse:&res error:&error];
if (error == nil) {
NSString *strData = [[NSString alloc]initWithData:ans encoding:NSUTF8StringEncoding];
NSLog(@"%@",strData);
}
ここで何がうまくいかないのかわかりません...助けてください..