2013-07-17 118 views
0

我尝试通过POST方法从服务器获取数据。AFJSONRequestOperation在模拟器上工作,但不在设备上

这里是我的代码:

AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://localhost:8080"]]; 
httpClient.operationQueue.maxConcurrentOperationCount = 1; 

NSDictionary *jsonDict = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"1", @"identity", 
          @"3", @"priority", 
          @"false", @"clearBadge", 
          @"1", @"pushNotificationID", 
          nil]; 

NSMutableURLRequest *signInRequest = [httpClient requestWithMethod:@"POST" path:@"/.../push/update" parameters:jsonDict]; 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:nil]; 

[signInRequest setHTTPBody: jsonData]; 

[signInRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
signInRequest.timeoutInterval = 15.0; 
signInRequest.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData; 

AFJSONRequestOperation *signInOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:signInRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
{ 
    NSLog(@"%@", JSON); 
} 
                          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) 
{ 
    NSLog(@"%@", [error userInfo]); 
}]; 

[httpClient enqueueHTTPRequestOperation:signInOperation]; 

和模拟器让我的数据和一切工作正常。 但是当我在设备上运行它,我得到这个错误信息:

2013-07-17 11:27:43.760 PushApp[11871:907] { 
NSErrorFailingURLKey = "http://localhost:8080/.../push/update"; 
NSErrorFailingURLStringKey = "http://localhost:8080/.../push/update"; 
NSLocalizedDescription = "Could not connect to the server."; 
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1004 \"Could not connect to the server.\" UserInfo=0x1e8351b0 {NSErrorFailingURLKey=http://localhost:8080/.../push/update, NSErrorFailingURLStringKey=http://localhost:8080/.../push/update, NSLocalizedDescription=Could not connect to the server.}";} 

什么想法?

回答

0

您正在连接到localhost:8080,因此必须在处理这些请求的“桌面”计算机上运行某个服务器。这不是真的在设备上(除非你已经做了一些事情才能使它成真)。

可能你想要做的是将韧体URL改为你的'桌面'机器的IP地址,并确保你的设备连接到同一个网络。

相关问题