Sandah Aung
Sandah Aung

Reputation: 6188

RestKit 404 error on ios

The following method calls the 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];
    }
}

This call should return JSON like this:

[{"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}]

Instead of receiving the JSON, the call is returned the following error:

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

It looks like a server error but the service can be called from other devices. Have I made any mistake in the code that calls the service?

Upvotes: 0

Views: 521

Answers (2)

gnasher729
gnasher729

Reputation: 52632

Status 404 is most definitely not a server error. You should really go just to Wikipedia, for example, and read up what all the http status codes mean, or you will be lost forever.

For starters, it is not an error. It is a status. It doesn't say something went wrong, it informs you what happened. 404 means: Resource not found. You asked for a resource, the server fully understood what you are looking for, and it found that whatever you are looking for just isn't there. If the data was there, it would have found it, but it wasn't there. There's not a hint of a server error.

Now obviously you might have made a mistake, and you are not looking for what you wanted to look for. That's your fault.

Upvotes: 0

user3350447
user3350447

Reputation:

Change

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

to

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

and

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

to

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

Upvotes: 1

Related Questions