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が応答の形で返され、画面に出力されることです。せいぜい、戻り値が null であることがわかりましたが、そうではないと確信しています。実際の例がありますが、辞書の代わりに配列を使用しています。結果は以下に添付されています。
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: 配列 ([0] => バナナ [1] => アップル [2] => いくつかのチーズ) ["バナナ"、"アップル"、"いくつかのチーズ"]
助けてください?本当にありがとうございました