2012-03-29 48 views
2

我正在努力將一些NSURLConnection代碼轉換爲AFNetworking,我看到一個奇怪的問題,它不會識別我的本地MAMP。AFNetworking不認識MAMP

以下是錯誤信息,我從NSURLConnection的

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x160cb0 {NSErrorFailingURLStringKey=http://localhost:8888/webservices/poll.php, NSErrorFailingURLKey=http://localhost:8888/webservices/poll.php, NSLocalizedDescription=Could not connect to the server., NSUnderlyingError=0x15f910 "Could not connect to the server."}

NSURLConnection的與甲基苯丙胺發揮得很好,我沒有連接到與AFNetworking遠程服務器的任何麻煩。此外,將失敗的URL粘貼到我的瀏覽器中工作得很好。

這裏是我使用

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8888/webservices/"]]; 
    [client getPath:@"poll.php" 
     parameters:nil 
      success:^(AFHTTPRequestOperation *operation, id response) { 
       NSString *text = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
       NSLog(@"Response: %@", text); 
      } 
      failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
       NSLog(@"Error"); 
       NSLog(@"%@", error);          
      }];

我可以從遠程服務器的工作代碼,但它會很高興能夠讓本地更改,而無需上載每個編輯測試文件。我已經看到幾個參考本地服務器的AFNetworking代碼片段。任何想法可能在這裏的問題?

回答

7

嘗試使用您的計算機的IP地址而不是本地主機。

+0

這樣做。我剛剛意識到我已經在設備上進行了測試(本地主機根本無法工作)。我懷疑從模擬器運行也會起作用。 – 2012-03-30 01:24:17

+0

我最近遇到了同樣的問題(MAMP,XCode 5,AFNetworking 2.0),並切換到IP地址解決了問題。然而,在*東西*改變之前(我不知道什麼/改變了)我能夠使用本地主機在設備上測試。 – JaredH 2013-10-05 20:24:28