2013-07-17 121 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地址,並確保你的設備連接到同一個網絡。

相關問題