我使用以下代碼將數組發送到php服務器,但在php端他們沒有收到任何值。我將數組作爲json字符串發送。如何使用web服務將json數組發佈到Php服務器
- (void)postArray {
//Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://test.com./t.php"]];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"one"];
[array addObject:@"Two"];
[array addObject:@"three"];
[array addObject:@"four"];
[request setHTTPMethod:@"POST"];
// NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:array,@"comment", nil];
SBJSON *parser =[[SBJSON alloc] init];
// NSString *jsonString = [parser stringWithObject:dict];
NSString *jsonString = [parser stringWithObject:array];
NSLog(@"Str %@",jsonString);
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[request setValue:jsonString forHTTPHeaderField:@"comment"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];
if (statusCode==200) {
//Request goes in success
NSError* error;
NSMutableDictionary* json = [NSJSONSerialization
JSONObjectWithData:data //1
options:kNilOptions
error:&error];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Json for post array ----------%@",str);
}else{
///request is get failed
NSLog(@"Error Description %@",[error description]);
}
}];
[request release];
}
與輸出在PHP是
<預> <BR>這是響應從iPhone <BR>首先<BR> <BR>端 首先<BR>第二接收JSON越來越響應和解碼json 響應<br>從commaArrayBR > nullThird斷裂( [0] =>)
這裏是PHP代碼
<?php
echo "<pre>";
echo "<br>";
//echo "Testing array";
$totalitem=$_POST['total'];
echo "This is response getting from the Iphone";
echo "<br>";
echo "First";
echo $jsonarray1=$_POST['comment'];
echo "<br>";
print_r($jsonarray1);
echo "<br>";
echo "end First";
echo "<br>";
echo "Second receive json response and decode the json response";
echo "<br>";
echo $comment=$_POST['comment'];
echo "<br>";
$jsonarray=json_decode($comment);
echo json_encode($jsonarray);
echo "Third Breaking from comma";
echo "<br>";
$allval=explode(',',$jsonarray1);
print_r($allval);
?>
請幫助
郵報出從PHP – Codesen
把這是應對來自iPhone 第一端得到第一個 第二個接收json響應並解碼json響應 null3rd從commaArray([0] =>) – iProgrammer
同樣msg他們重定向到iphone – iProgrammer