繁体   English   中英

AFJSONRequestOperation在模拟器上起作用,但在设备上不起作用

[英]AFJSONRequestOperation works on simulator but not on device

我尝试通过POST方法从服务器获取数据。

这是我的代码:

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.}";}

有任何想法吗?

您正在连接到localhost:8080因此必须在处理这些请求的“台式”计算机上运行某些服务器。 这在设备上是不正确的(除非您做了一些使其正确的事情)。

可能您想做的就是将bast URL更改为“台式”计算机的IP地址,并确保您的设备连接到同一网络。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM