我在 PHP 中有以下脚本:
<?php $sender = $_POST['sender'];
$rcpt = $_POST['rcpt'];
$message = $_POST['message'];
$someArray = array("Bannana", "Apple", "SomeCheese");
print_r($someArray);
echo json_encode($someArray);
?>
以及以下按钮操作:
NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];
/**********Set Request properties*************/
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSUInteger content = [NSString stringWithUTF8String:[returnedDictionary count]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Count: %u", content];
我想要实现的是,被回显的 json 应该以响应的形式返回并打印到屏幕上。充其量我得到的回报是空的,我确信它不是。我有一个工作示例,但使用数组而不是字典,结果附在下面:
NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://development.com/ios/responseScript.php" ] ];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
//NSLog(@"responseData: %@", content);
self.responseLabel.text = [NSString stringWithFormat:@"Data: %@", content];
结果:
2013-01-30 19:07:07.919 testResponse[15921:c07] responseData: Array ([0] => Bannana [1] => Apple [2] => SomeCheese) ["Bannana","Apple","SomeCheese "]
请帮忙?十分感谢