I’m working on converting some NSURLConnection code over to AFNetworking and I’m seeing a strange issue where it won’t recognize my local MAMP.
Here is the error message I get from 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 was playing nicely with MAMP and I don’t have any trouble connecting to remote servers with AFNetworking. Further, pasting the failed URL into my browser works just fine.
Here is the code I’m using
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);
}];
I can work from a remote server, but it would be nice to be able to make changes locally without having to upload files on every edit to test. I’ve seen several snippets of AFNetworking code that reference local servers. Any ideas what the issue might be here?
try using the ip address of your computer instead of localhost.