在ios上出现RestKit 404错误

时间:2015-07-19 11:34:40

标签: ios restkit-0.20

以下方法调用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];
    }
}

此调用应返回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:

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

它看起来像是服务器错误,但可以从其他设备调用该服务。我在调用服务的代码中犯了什么错误吗?

2 个答案:

答案 0 :(得分:1)

更改

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)];

答案 1 :(得分:0)

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

对于初学者来说,这不是一个错误。这是一种状态。它并没有说出错了,它告诉你发生了什么。 404表示:未找到资源。你问了一个资源,服务器完全理解你在寻找什么,它发现无论你在寻找什么都没有。如果数据存在,它会找到它,但它不存在。没有提示服务器错误。

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