简体   繁体   English

来自XCode iphone模拟器中localhost的JSON响应

[英]JSON response from localhost in XCode iphone Simulator

I am trying to receive a JSON response in iPhone simulator using localhost web service. 我正在尝试使用本地主机Web服务在iPhone模拟器中接收JSON响应。

I turned on Web sharing and placed the php file named "readmain.php" in /Library/WebServer/Documents 我打开了Web共享,并将名为“ readmain.php”的php文件放在/ Library / WebServer / Documents中

When i write "http://localhost/readmain.php" in Chrome, I see the result displayed perfectly. 当我在Chrome中写“ http://localhost/readmain.php”时,我看到结果显示完美。

But when I try to receive the response in my app, an error is received. 但是,当我尝试在我的应用程序中接收响应时,会收到错误消息。

I am using ASIHTTPRequest 我正在使用ASIHTTPRequest

Here's the code: 这是代码:

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

Where HJServerAddress is: #define HJServerAddress @"http://localhost/" 其中HJServerAddress为:#define HJServerAddress @“ http:// localhost /”

Whenever the above piece of code executes, I get response from as failed in: 每当执行以上代码时,我都会收到以下失败的响应:

   - (void)requestFailed:(ASIHTTPRequest *)request
   {
       DLog(@"error: %@", [request.error description])
       ULog(@"Failed to load data")
   }

In Logs I get: 在日志中,我得到:

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}

I had also tried to use the IP address of my MAC instead of using "localhost". 我还尝试过使用MAC的IP地址,而不是使用“ localhost”。

I tried accessing the file from iOS simulator's Safari and the response was received successfully. 我尝试从iOS模拟器的Safari访问文件,并成功接收到响应。

I am unable to receive the response in my underdevelopment app. 我在开发不足的应用中无法收到回复。 Am I doing something wrong in my code? 我在代码中做错了吗? Or is there something more to be done? 还是还有其他事情要做?

Thanks! 谢谢!

您将NSString对象分配给NSURL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM