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 应该以响应的形式返回并打印到屏幕上。充其量我得到的回报是空的,我确信它不是。我有一个工作示例,但使用数组而不是字典,结果附在下面:

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 "]

请帮忙?十分感谢

4

2 回答 2

1

这条线有问题:

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

sendSynchronousRequest:returningResponse:error:返回 NSData,而不是 NSDictionary。

阅读文档,确保你知道你正在处理什么类型,祝你好运。

于 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 回答