0

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] => いくつかのチーズ) ["バナナ"、"アップル"、"いくつかのチーズ"]

助けてください?本当にありがとうございました

4

2 に答える 2

1

この行には問題があります。

NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

sendSynchronousRequest:returningResponse:error:NSDictionaryではなくNSDataを返します。

ドキュメントを読んで、あなたが扱っているタイプと幸運を知っていることを確認してください。

于 2013-01-30T16:15:30.400 に答える
0
NSDictionary *returnedDictionary = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSDataオブジェクトではなく、ここでオブジェクトを取得していNSDictionaryます。

NSUInteger content = [NSString stringWithUTF8String:[returnedDictionary count]];

ここでは、その内容ではなく、(存在しない) 辞書の項目数を取得しています。

このコードでユーザー インターフェイス要素を更新していることを考えると、メイン スレッドで実行されていると想定しています。メイン スレッドで同期要求を行うべきではありません。ネットワーク上で待機している間にアプリケーションがハングするだけです。バックグラウンド スレッドで同期要求を行うか、メイン スレッドで非同期要求を行います。

そこから、使用しているクラスのドキュメントを参照し、Xcode が確実に表示している警告に注意してください。

于 2013-01-30T16:19:33.293 に答える