ios - iOS 上的 RestKit 404 错误

标签 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

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

最佳答案

改变

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

关于ios - iOS 上的 RestKit 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500757/

相关文章:

iOS RestKit POST 未修改的 JSON 字符串

ios - RestKit .20 核心数据 postObject EXC_BAD_ACCESS

ios - UIImageView 的动画在真实的 iOS 设备上效果不佳

ios - icroll.js 子元素溢出 : scroll not working on IOS

ios - 当没有执行任何操作并且用户点击通知时如何处理交互式通知?

iphone - 如何对 iphone 通讯录进行排序?

访问照片库时 iOS 10.3 WKWebView 崩溃

ios - 在 RestKit 中使用 RKObjectManager 从 CoreData 获取数据

ios - 自签名证书是否被视为无效?

mapping - 如何使用此 JSON 实现动态映射 (RKDynamicMapping)?