ios 上的 RestKit 404 错误

RestKit 404 error on ios

以下方法调用 Rest API (http://128.199.186.47:8080/DenkoStationServer-1/denkoservice/price).

- (void)displayInternetConnectivityMessage {
    NetworkStatus netStatus = [self.internetReachability currentReachabilityStatus];

    if (netStatus == NotReachable)      {
        if ([self isMyanmar])
            [self.view makeToast:@"tifwmeuf zGifhrxm;yg"];
        else
            [self.view makeToast:@"No internet connection"];        
    } else {
        NSURL *baseURL = [NSURL URLWithString:@"http://128.199.186.47:8080/DenkoStationServer-1/denkoservice"];
        NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];

        RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Price class]];
        [mapping addAttributeMappingsFromDictionary:@{@"id": @"ron95", @"ron95": @"ron92", @"dieselNormal": @"dieselNormal", @"dieselSpecial": @"dieselSpecial", @"postDate": @"postDate"}];

        RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:nil keyPath:@"price" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

        RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
        [objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            if ([self isMyanmar])
                [self.view makeToast:@"qmAmESifh csdwfqufjcif; atmifjrifonf"];
            else
                [self.view makeToast:@"Server connection successful"];
            [self updateLocalPrice];
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"error: %@", [error localizedDescription]);
            if ([self isMyanmar])
                [self.view makeToast:@"qmAmESifh qufoG,fr&yg"];
            else
                [self.view makeToast:@"Connection with server failed"];
        }];

        [objectRequestOperation start];
    }
}

这个调用应该 return JSON 像这样:

[{"id":1,"ron95":700.0,"ron92":700.0,"dieselNormal":700.0,"dieselSpecial":700.0,"postDate":1435465383000},
{"id":2,"ron95":800.0,"ron92":730.0,"dieselNormal":700.0,"dieselSpecial":730.0,"postDate":1435819788000}]

调用没有收到 JSON,而是 returned 以下错误:

2015-07-19 17:54:19.334 DenkoStation[35344:1002137] I restkit:RKLog.m:49 RestKit logging initialized...
2015-07-19 17:54:19.836 DenkoStation[35344:1002137] Presenting view controllers on detached view controllers is discouraged <RootViewController: 0x7fc042f31eb0>.
2015-07-19 17:54:19.848 DenkoStation[35344:1002137] Reachability Flag Status: -R -----l- networkStatusForFlags
2015-07-19 17:54:19.883 DenkoStation[35344:1002137] h=568
2015-07-19 17:54:19.883 DenkoStation[35344:1002137] scale=2.000000
2015-07-19 17:54:19.919 DenkoStation[35344:1002137] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://128.199.186.47:8080/DenkoStationServer-1/denkoservice'
2015-07-19 17:54:21.992 DenkoStation[35344:1002218] E restkit.network:RKObjectRequestOperation.m:551 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0x7fc042d8dbb0 {NSLocalizedRecoverySuggestion=<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /DenkoStationServer-1/denkoservice. Reason:
<pre>    Not Found</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>
, NSErrorFailingURLKey=http://128.199.186.47:8080/DenkoStationServer-1/denkoservice, AFNetworkingOperationFailingURLRequestErrorKey=<NSURLRequest: 0x7fc042d80430> { URL: http://128.199.186.47:8080/DenkoStationServer-1/denkoservice }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7fc042c34300> { URL: http://128.199.186.47:8080/DenkoStationServer-1/denkoservice } { status code: 404, headers {
    "Cache-Control" = "must-revalidate,no-cache,no-store";
    "Content-Length" = 1398;
    "Content-Type" = "text/html;charset=ISO-8859-1";
    Date = "Sun, 19 Jul 2015 11:24:21 GMT";
    Server = "Jetty(8.1.17.v20150415)";
} }, NSLocalizedDescription=Expected status code in (200-299), got 404}
2015-07-19 17:54:21.993 DenkoStation[35344:1002217] E restkit.network:RKObjectRequestOperation.m:215 GET 'http://128.199.186.47:8080/DenkoStationServer-1/denkoservice' (404 Not Found / 0 objects) [request=2.0702s mapping=0.0000s total=2.1423s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 404" UserInfo=0x7fc042d8dbb0 {NSLocalizedRecoverySuggestion=<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /DenkoStationServer-1/denkoservice. Reason:
<pre>    Not Found</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>
, NSErrorFailingURLKey=http://128.199.186.47:8080/DenkoStationServer-1/denkoservice, AFNetworkingOperationFailingURLRequestErrorKey=<NSURLRequest: 0x7fc042d80430> { URL: http://128.199.186.47:8080/DenkoStationServer-1/denkoservice }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7fc042c34300> { URL: http://128.199.186.47:8080/DenkoStationServer-1/denkoservice } { status code: 404, headers {
    "Cache-Control" = "must-revalidate,no-cache,no-store";
    "Content-Length" = 1398;
    "Content-Type" = "text/html;charset=ISO-8859-1";
    Date = "Sun, 19 Jul 2015 11:24:21 GMT";
    Server = "Jetty(8.1.17.v20150415)";
} }, NSLocalizedDescription=Expected status code in (200-299), got 404}
2015-07-19 17:54:21.994 DenkoStation[35344:1002137] error: Expected status code in (200-299), got 404

看起来像是服务器错误,但可以从其他设备调用该服务。我是不是调用服务的代码出错了?

改变

NSURL *baseURL = [NSURL URLWithString:@"http://128.199.186.47:8080/DenkoStationServer-1/denkoservice"];

NSURL *baseURL = [NSURL URLWithString:@"http://128.199.186.47:8080/DenkoStationServer-1/denkoservice/price"];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:nil keyPath:@"price" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

状态 404 绝对不是服务器错误。例如,你真的应该去维基百科,阅读所有 http 状态代码的含义,否则你将永远迷失方向。

对于初学者来说,这不是错误。这是一种状态。它并没有说出了什么问题,它会告诉您发生了什么。 404 表示:找不到资源。您请求资源,服务器完全理解您要查找的内容,但它发现您要查找的内容根本不存在。如果数据在那里,它会找到它,但它不在那里。没有服务器错误的提示。

现在显然你可能犯了一个错误,你不是在寻找你想要寻找的东西。那是你的错。