0

サーバーから 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.}";}

何か案は?

4

1 に答える 1

0

接続してlocalhost:8080いるため、これらのリクエストを処理している「デスクトップ」マシンで実行されているサーバーが必要です。それはデバイス上では正しくありません (それを実現するために何かを行った場合を除きます)。

おそらくやりたいことは、bast URL を「デスクトップ」マシンの IP アドレスに変更し、デバイスが同じネットワークに接続されていることを確認することです。

于 2013-07-17T10:32:09.620 に答える