Restkit returning nil response from Foursquare API - ios

I am trying to learn RestKit through "http://www.raywenderlich.com/58682/introduction-restkit-tutorial" and was simultaneously trying out the sample code provided on the site. Well my main problem is that the RestKit is not returning the response even though the request I send to API is correct and I get the response when I test it through browser.
Here is the code from the site (only change is I am sending in some other param):
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:#"https://api.foursquare.com"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]];
[venueMapping addAttributeMappingsFromArray:#[#"name"]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:venueMapping
method:RKRequestMethodGET
pathPattern:#"/v2/venues/search"
keyPath:#"response.venues"
statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadVenues
{
NSString *near = #"Chicago, IL";
NSString *clientID = kCLIENTID;
NSString *clientSecret = kCLIENTSECRET;
NSDictionary *queryParams = #{#"near" : near,
#"client_id" : clientID,
#"client_secret" : clientSecret,
#"categoryId" : #"4bf58dd8d48988d1e0931735",
#"v" : #"20140118"};
[[RKObjectManager sharedManager] getObjectsAtPath:#"/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
_venues = mappingResult.array;
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"What do you mean by 'there is no coffee?': %#", error);
}];
}
Here is the API request URL:
https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=kCLIENTID&client_secret=kCLIENTSECRET&near=Chicago%2C%20IL&v=20150110
My 'mappingResult' object in the success block is coming out to be:
<RKMappingResult: 0x7cb4f6d0, results={
"response.venues" = (
);
}>
and 'operation' object is:
<RKObjectRequestOperation: 0x78e697c0, state: Successful, isCancelled=NO, request: <NSMutableURLRequest: 0x78e4f2f0> { URL: https://api.foursquare.com/v2/venues/search?categoryId=4bf58dd8d48988d1e0931735&client_id=kCLIENTID&client_secret=kCLIENTSECRET&near=Chicago%2C%20IL&v=20150110 }, response: <NSHTTPURLResponse: 0x79a46e40 statusCode=200 MIMEType=application/json length=35120>>
Any idea what is going on?
P.S. To test the URL I provided, you will need to replace kCLIENTID and kCLIENTSECRET with your client id and client secret info.

Change the queryParams to
NSString *ll = #"40.7,-74.20";
NSDictionary *queryParams = #{#"ll": ll,
#"client_id" : clientID,
#"client_secret" : clientSecret,
#"categoryId" : #"4bf58dd8d48988d1e0931735",
#"v": #"20140118"
};

Related

Restkit failed to match response descriptors

This is my JSON: link
This is my Mapper:
-(RKObjectManager *)mapContact{
RKObjectMapping* dataMapping = [RKObjectMapping mappingForClass:[Data class]];
[dataMapping addAttributeMappingsFromDictionary:#{
#"Data": #"app_data",
#"message": #"app_message",
#"status":#"app_status",
#"Success": #"app_success"
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dataMapping method:RKRequestMethodGET pathPattern:nil keyPath:#"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKEntityMapping* contactMapping = [RKEntityMapping mappingForEntityForName:#"Contact" inManagedObjectStore:managedObjectStore];
contactMapping.identificationAttributes = #[#"con_id"];
[contactMapping addAttributeMappingsFromDictionary:#{
#"content": #"con_content",
#"id" : #"con_id",
#"title" : #"con_title",
#"language":#"con_language",
}];
RKResponseDescriptor *responseDescriptor2 = [RKResponseDescriptor responseDescriptorWithMapping:contactMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:#"data.contentblock"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor,responseDescriptor2,nil];
[objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
return objectManager;
}
And this is my webservice call:
-(void)fetchContactOnCompletion:(myCompletion) compblock{
Mapper *mapper = [Mapper new];
RKManagedObjectStore *store = [[ASLDataModel sharedDataModel] objectStore];
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
RKObjectManager *objectManager = [mapper mapContact];
NSString *urlString = [NSString stringWithFormat:#"webservice/content/get/apikey/%#/language/%#/contentid/2",apikey,language];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil];
RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
Data *data = [mappingResult.dictionary valueForKey:#""];
if([data.app_status isEqualToNumber:#200]){
NSError *error = nil;
BOOL success = [context save:&error];
if (!success) RKLogWarning(#"Failed saving managed object context: %#", error);
NSError *saveError = nil;
compblock(YES);
}else{
compblock(NO);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
[objectManager enqueueObjectRequestOperation:operation];
}
And I get these errors:
2015-09-08 08:38:56.431 ASLTravel[17377:427756] D restkit.object_mapping:RKMapperOperation.m:433 Finished performing object mapping. Results: (null)
2015-09-08 08:38:56.432 ASLTravel[17377:427753] E restkit.network:RKObjectRequestOperation.m:209 GET 'http://asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' (200 OK / 0 objects) [request=1.6674s mapping=0.0000s total=1.6899s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x7f9413f8b730 {NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2', which failed to match all (2) response descriptors:
<RKResponseDescriptor: 0x7f9413f535e0 baseURL=http://asl-travel.be/ pathPattern=(null) statusCodes=200-299> failed to match: response URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' is not relative to the baseURL 'http://asl-travel.be/'.
<RKResponseDescriptor: 0x7f9413f2b1e0 baseURL=http://asl-travel.be/ pathPattern=(null) statusCodes=200-299> failed to match: response URL 'http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2' is not relative to the baseURL 'http://asl-travel.be/'., NSErrorFailingURLStringKey=http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2, NSErrorFailingURLKey=http://www.asl-travel.be/webservice/content/get/apikey/asl001/language/nl/contentid/2, NSUnderlyingError=0x7f9413c2fc20 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}
Can anyone please help me with this? I'm searching for this problem for hours now...
Many thanks !
The web service is redirecting you to www.asl-travel.be so the pattern matcher will never find a match. Update the base URL or turn off the redirection to fix this.

Restkit basic url response

I am learning Rest-kit. i am trying to parse this url https://api.coursera.org/api/catalog.v1/courses?fields=language,shortDescription
I have created one Courses class.
#interface Courses : NSObject
#property (nonatomic, strong) NSString *name;
#end
and in viewcontroller i have wrote below code
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureRestKit];
[self loadCourses];
}
- (void)configureRestKit
{
// https://api.coursera.org/api/catalog.v1/courses?fields=language,shortDescription
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:#"https://api.coursera.org"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
// setup object mappings
RKObjectMapping *courseMapping = [RKObjectMapping mappingForClass:[Courses class]];
[courseMapping addAttributeMappingsFromDictionary:#{
#"name":#"name"
}];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:courseMapping method:RKRequestMethodGET pathPattern:#"/api/catalog.v1/courses?fields=language,shortDescription" keyPath:#"elements" statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
}
- (void)loadCourses
{
[[RKObjectManager sharedManager] getObjectsAtPath:#"/api/catalog.v1/courses?fields=language,shortDescription" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"%#", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"What do you mean by 'there is no Courses?': %#", error);
}];
}
ERROR :_Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded."
I am not getting response in success block. Please correct me. Thanks in advance
Don't add your query parameters to the path pattern on the response descriptor, and set them as parameters on the request:
pathPattern:#"/api/catalog.v1/courses"
generally I would also say that /api/catalog.v1/ should be part of the base URL so you would have:
NSURL *baseURL = [NSURL URLWithString:#"https://api.coursera.org/api/catalog.v1/"];
...
... pathPattern:#"courses" ...
...
... getObjectsAtPath:#"courses" parameters:#{ #"fields" : #"language,shortDescription" } ...

RestKit Put service is not affecting database data on server even getting response is success

I am developing iPhone application and using Restkit for consuming services and put data on server. I am working with Restkit first time. I want to put data on server then I implemented the restkit put method, and it executed successfully and getting response "SUCCESS" but when I retrieving data then it doesn't affecting data means not getting last put data on server. So can anybody tell me what is wrong with this?? I used the following code I used :
////Response Mapping values
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[NSMutableArray class]];
[responseMapping addAttributeMappingsFromArray:#[#"SenderId", #"SentDate", #"Status",#"GroupId", #"Message"]];
//Request Mapping values
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromArray:#[#"SenderId", #"SentDate", #"Status",#"GroupId", #"Message"]];
ITRestKitServices *restkitService = [[ITRestKitServices alloc] initWithDelegate:self];
[restkitService putDataONserverBulletin:bulletinMutableArray pathPattern:#"bulletin" keyPath:#"" userName:appDelegate.user.Email password:appDelegate.user.SessionToken requestMapping:requestMapping responseMapping:responseMapping];
// And following code to put data on server in service I used:
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:BASE_URL_STRING];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client setAuthorizationHeaderWithUsername:userName password:password];
//set pathPattern
NSString *path = [NSString stringWithFormat:#"%#/%#",BASE_PATH_STRING,pathPattern];
//StatusCodes
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
//Request Descriptor
RKRequestDescriptor *requestDescriptor =
[RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Bulletin class]
rootKeyPath:nil
method:RKRequestMethodPUT];
//Response Descriptor
RKResponseDescriptor *userResponseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodPUT
pathPattern:path
keyPath:keyPath
statusCodes:statusCodes];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[objectManager addRequestDescriptor:requestDescriptor];
[objectManager addResponseDescriptor:userResponseDescriptor];
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
if(appDelegate.isLoading == NO)
{
[appDelegate startLoading];
}
[objectManager putObject:dataObject
path:path
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(#"Bulletin Success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
if(appDelegate.isLoading == YES)
{
[appDelegate stopLoading];
}
NSLog(#">>> Error: %#", error);
}];

this class is not key value coding-compliant for the key Text using RestKit v0.20.0

For 2 days now, I've been trying to find out why I'm getting the error using iOS 6.1.3 with Xcode 4.6.2 and RestKit 0.20.0:
"...this class is not key value coding-compliant for the key Text."
The strange part is that I can receive (GET) the JSON object fine. The error happens when I create my sample SignalMessage object and then try to PUT it back to the server.
The JSON is as follows:
{"Text":"New Message","HasMessage":"true"}
The SignalMessage object looks like this:
#import <Foundation/Foundation.h>
#interface SignalMessage : NSObject {
}
#property (nonatomic, copy) NSString *signalText;
#property (nonatomic, retain) NSNumber *isHasMessage;
#end
And the implementation like this:
#import "SignalMessage.h"
#implementation SignalMessage
#synthesize isHasMessage, signalText;
#end
My correctly working getMessage function looks like this:
- (IBAction)getMessage:(id)sender;
{
NSLog(#"%#", #"Getting message... ");
NSURL *url = [NSURL URLWithString:#"http://ec2-54-243-148-145.compute-1.amazonaws.com/TabletPractice/api/signal?clientIdentifier=2"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[SignalMessage class]];
[responseMapping addAttributeMappingsFromDictionary:#{#"Text":#"signalText", #"HasMessage": #"isHasMessage"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];
[manager getObject:nil path:#"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
NSArray *theresults = [result array];
for (SignalMessage *item in theresults) {
self.txtMessage.text = item.signalText;
[self hideControls];
}
} failure:^(RKObjectRequestOperation * operation, NSError * error)
{
NSLog (#"Server WS call failure: operation: %# \n\nerror: %#", operation, error);
}];
}
And here is the sendClicked message that gives me grieve:
- (IBAction)btnSendClicked:(id)sender;
{
if ([txtMessage.text length] < 1)
return;
NSURL *url = [NSURL URLWithString:#"http://ec2-54-243-148-145.compute-1.amazonaws.com/TabletPractice/api/signal?clientIdentifier=2"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:#{#"Text":#"signalText", #"HasMessage": #"isHasMessage"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[SignalMessage class]
rootKeyPath:#""];
[manager addRequestDescriptor:requestDescriptor];
SignalMessage *newMessage = [[SignalMessage alloc] init];
newMessage.signalText = #"Test Message";
BOOL isMsg = TRUE;
NSNumber *boolAsNumber = [NSNumber numberWithBool:isMsg];
newMessage.isHasMessage = boolAsNumber;
[manager putObject:newMessage path:#"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(#"We object mapped the response with the following result: %#", result);
} failure:^(RKObjectRequestOperation * operation, NSError * error)
{
NSLog (#"Server WS call failure: operation: %# \n\nerror: %#", operation, error);
}];
[self hideControls];
}
At this point, I'm at a loss.
Please add a inverse mapping to your RKRequestDescriptor in your btnSendClicked method like below:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor
requestDescriptorWithMapping:[requestMapping inverseMapping]
objectClass:[SignalMessage class]
rootKeyPath:#""];

Json Mapping using Restkit v0.20

i am able to send request correctly but after receiving response i get
response.body ={"status":"success","loginToken":"xxxxyyyzzz"}
but with an error saying
It Failed: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors
match the response loaded." UserInfo=0x2b55b0 {NSErrorFailingURLStringKey=http://192.168.1.71
/firstyii/index.php?r=site/login, NSLocalizedFailureReason=A 200 response was loaded from the
URL 'http://192.168.1.71/firstyii/index.php?r=site/login', which failed to match all (1)
response descriptors:
<RKResponseDescriptor: 0x2a5860 baseURL=http://192.168.1.71 pathPattern=/firstyii
/index.php?r=site/login statusCodes=200-299> failed to match: response path '/firstyii
/index.php?r=site/login' did not match the path pattern '/firstyii/index.php?r=site/login'.,
NSLocalizedDescription=No response descriptors match the response loaded., keyPath=null,
NSErrorFailingURLKey=http://192.168.1.71/firstyii/index.php?r=site/login,
NSUnderlyingError=0x2b6070 "No mappable object representations were found at the key paths
searched."}
LoginResponse.h
#interface LoginResponse : NSObject
#property (nonatomic, strong) NSString *status;
#property (nonatomic, strong) NSString *loginToken;
+(RKObjectMapping*)defineLoginResponseMapping;
#end
LoginResponse.m
#import "LoginResponse.h"
#implementation LoginResponse
#synthesize loginToken;
#synthesize status;
+(RKObjectMapping*)defineLoginResponseMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LoginResponse class]];
[mapping addAttributeMappingsFromDictionary:#{
#"status": #"status",
#"loginToken": #"loginToken",
}];
return mapping;
}
#end
Code in LoginManager.m
-(void)LoginWithUserName:(NSString *)username password:(NSString*)password {
LoginRequest *dataObject = [[LoginRequest alloc] init];
[dataObject setUsername:username];
[dataObject setPassword:password];
NSURL *baseURL = [NSURL URLWithString:#"http://192.168.1.71"];
AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:baseURL];
[client setDefaultHeader:#"Accept" value:RKMIMETypeJSON];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class]
forMIMEType:#"application/json"];
RKObjectMapping *requestMapping = [[LoginRequest defineLoginRequestMapping] inverseMapping];
[objectManager addRequestDescriptor: [RKRequestDescriptor
requestDescriptorWithMapping:requestMapping objectClass:[LoginRequest class] rootKeyPath:nil
]];
// what to print
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);
RKObjectMapping *responseMapping = [LoginResponse defineLoginResponseMapping];
[objectManager addResponseDescriptor:[RKResponseDescriptor
responseDescriptorWithMapping:responseMapping pathPattern:#"/firstyii/index.php?r=site/login"
keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[objectManager setRequestSerializationMIMEType: RKMIMETypeJSON];
[objectManager postObject:dataObject path:#"/firstyii/index.php?r=site/login"
parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"It Worked: %#", [mappingResult array]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"It Failed: %#", error);
}];
}
i think that the code used above is to map response with array ,
so i need code to map simple json like this one response.body={"status":"success","loginToken":"logingngngnnggngngae"}
Such responses are to be mapped using pathpattern , non keyvalue mapping mentioned at https://github.com/RestKit/RestKit/wiki/Object-mapping.
I was using pathPattern having "?" in it , hence it was not matching properly.
,also path pattern shouldnot have "/" at beginning.
the above code works fine if i change the pathPattern to pathPattern:#"site/login"
,and postObject to postObject:#"site/login"
and ofcourse changed the baseurl = #"http://json.xxxxxxx.xxxxx:8179/jsonresponse/index.php" so that there are not special charaters in it.

Resources