[英]JSON response from localhost in XCode iphone Simulator
我正在尝试使用本地主机Web服务在iPhone模拟器中接收JSON响应。
我打开了Web共享,并将名为“ readmain.php”的php文件放在/ Library / WebServer / Documents中
当我在Chrome中写“ http://localhost/readmain.php”时,我看到结果显示完美。
但是,当我尝试在我的应用程序中接收响应时,会收到错误消息。
我正在使用ASIHTTPRequest
这是代码:
NSURL *url = [NSString stringWithFormat:@"%@%@", HJServerAddress, @"readmain.php"];
DLog(@"request URL = %@", [url description])
self.httpRequest = [ASIHTTPRequest requestWithURL:url];
self.httpRequest.delegate = self;
[self.httpRequest startAsynchronous];
[self.activityIndicator startAnimating];
其中HJServerAddress为:#define HJServerAddress @“ http:// localhost /”
每当执行以上代码时,我都会收到以下失败的响应:
- (void)requestFailed:(ASIHTTPRequest *)request
{
DLog(@"error: %@", [request.error description])
ULog(@"Failed to load data")
}
在日志中,我得到:
2012-09-24 17:44:06.153 _APPNAME_[1798:f803] -[HJHomeViewController fetchAddsJson] [Line 122] request URL = "http://localhost/readmain.php"
2012-09-24 17:44:46.457 _APPNAME_[1798:f803] -[HJHomeViewController requestFailed:] [Line 478] error: Error Domain=ASIHTTPRequestErrorDomain Code=10 "NSInvalidArgumentException" UserInfo=0x69656e0 {NSLocalizedFailureReason=-[__NSCFString absoluteURL]: unrecognized selector sent to instance 0xb650510, NSUnderlyingError=0x6960460 "The operation couldn’t be completed. (ASIHTTPRequestErrorDomain error 10.)", NSLocalizedDescription=NSInvalidArgumentException}
我还尝试过使用MAC的IP地址,而不是使用“ localhost”。
我尝试从iOS模拟器的Safari访问文件,并成功接收到响应。
我在开发不足的应用中无法收到回复。 我在代码中做错了吗? 还是还有其他事情要做?
谢谢!
您将NSString对象分配给NSURL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.