簡體   English   中英

來自XCode iphone模擬器中localhost的JSON響應

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM