Objective-C と iOS の開発は初めてです。
iPhone から PHPに送信し、NSMutableArray
再度取得して NSLog を使用して印刷し、この配列が正常に送信されたことを確認したいと考えています。
次のコードを使用しました。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *phpUrl = @"http://dt-works.com/eman/bookMarks.php";
NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
NSMutableArray *arrayValue1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
NSMutableArray *arrayValue2 = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", nil];
NSDictionary *theReqDictionary1 = [NSDictionary dictionaryWithObjects:arrayValue1 forKeys:arrayKey];
NSDictionary *theReqDictionary2 = [NSDictionary dictionaryWithObjects:arrayValue2 forKeys:arrayKey];
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:theReqDictionary1,theReqDictionary2, nil];
NSString *jsonString = [myArray JSONRepresentation];
NSLog(@"%@", jsonString);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]];
NSString *post = [[NSString alloc] initWithFormat:@"jsonString=%@&submit=", jsonString];
//NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
int arraySize = [statuses count];
NSMutableArray *number = [[NSMutableArray alloc] init];
for (NSDictionary *status in statuses)
{
NSString *onee = [status objectForKey:@"one"];
NSString *twoo = [status objectForKey:@"two"];
NSString *threee = [status objectForKey:@"three"];
[number addObject:status];
NSLog(@"------------------ from browser-----------------------------------");
NSLog(@"myOne %@ - myTwo: %@ - myThree: %@ ",onee ,twoo, threee);
}
NSLog(@"size of array is: %d", arraySize);
NSLog(@"size of them is: %d", [number count]);
}
PHP で次のコードを書きました。
<?php
header('Content-type:text/html;charset=utf-8');
if (isset($_POST['submit'])) {
$jsonString = $_POST['jsonString'];
print_r($jsonString);
}
?>
出力は次のとおりです。
[{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}]
size of array is: 0
size of them is: 0
配列のサイズは 2 である必要があります。コードのどこが間違っていますか ???