我是 Objective-c 和 iOS 开发的新手。
我想NSMutableArray
从 iPhone 向 PHP 发送一个,然后再次取回并使用 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,我的代码有什么问题???