- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 RestKit,这是我初始化它并添加路由和描述符的方式:
- (void)initRestClient
{
NSURL *baseURL = [NSURL URLWithString:kSomeBaseURL];
self.manager = [RKObjectManager managerWithBaseURL:baseURL];
[self.manager setRequestSerializationMIMEType:RKMIMETypeJSON];
[self.manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[self.manager addResponseDescriptorsFromArray:[RKObjectManager sharedManager].responseDescriptors];
[self.manager addRequestDescriptorsFromArray:[RKObjectManager sharedManager].requestDescriptors];
[self.manager.HTTPClient.operationQueue setMaxConcurrentOperationCount:5];
[RKObjectManager setSharedManager:self.manager];
// AFHTTPClient *client = [RKObjectManager sharedManager].HTTPClient;
[self initRoutes];
[self initMappingObjectsAndDiscriptors];
}
- (void)initRoutes
{
RKRoute *bannersRoute = [RKRoute routeWithClass:[RKBanner class] pathPattern:@"Banners?categoryID=:categoryID" method:RKRequestMethodGET];
bannersRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:bannersRoute];
RKRoute *branchesRoute = [RKRoute routeWithClass:[RKBranches class] pathPattern:@"Branches?city=:city&type=:type" method:RKRequestMethodGET];
branchesRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:branchesRoute];
RKRoute *shortTokenRoute = [RKRoute routeWithClass:[RKShortToken class] pathPattern:@"users/login/quick/shortToken?phone=:phone&extraCode=:extraCode" method:RKRequestMethodGET];
shortTokenRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:shortTokenRoute];
RKRoute *longTokenRoute = [RKRoute routeWithClass:[RKLongToken class] pathPattern:@"users/login/quick/userDetails?shortToken=:shortToken" method:RKRequestMethodGET];
longTokenRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:longTokenRoute];
}
- (void)initMappingObjectsAndDiscriptors
{
RKObjectMapping *bannerMapping = [RKObjectMapping mappingForClass:[RKBanner class]];
[bannerMapping addAttributeMappingsFromDictionary:[RKBanner getAttributes]];
RKResponseDescriptor *bannerDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bannerMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"Banners" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectMapping *branchesMapping = [RKObjectMapping mappingForClass:[RKBranches class]];
[branchesMapping addAttributeMappingsFromDictionary:[RKBranches getAttributes]];
RKResponseDescriptor *branchesDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:branchesMapping method:RKRequestMethodGET pathPattern:nil keyPath:@"Branches" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectMapping *shortTokenMapping = [RKObjectMapping mappingForClass:[RKShortToken class]];
[shortTokenMapping addAttributeMappingsFromDictionary:[RKShortToken getAttributes]];
RKResponseDescriptor *shortTokenDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:shortTokenMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectMapping *longTokenMapping = [RKObjectMapping mappingForClass:[RKLongToken class]];
[longTokenMapping addAttributeMappingsFromDictionary:[RKLongToken getAttributes]];
// longTokenMapping.setDefaultValueForMissingAttributes = YES;
// longTokenMapping.setNilForMissingRelationships = YES;
RKResponseDescriptor *longTokenDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:longTokenMapping method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self.manager addResponseDescriptorsFromArray:@[bannerDescriptor, branchesDescriptor, shortTokenDescriptor, longTokenDescriptor]];
}
这是每个类的构建方式:
@interface RKBanner : NSObject
@property (strong, nonatomic) NSNumber *idNum;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *addressURL;
@property (strong, nonatomic) NSString *bannerPosition;
@property (strong, nonatomic) NSString *imageALT;
@property (strong, nonatomic) NSNumber *imageHeight;
@property (strong, nonatomic) NSString *imageURL;
@property (strong, nonatomic) NSNumber *imageWidth;
@property (strong, nonatomic) NSString *subtitle;
@property (strong, nonatomic) NSString *targetURL;
@property (strong, nonatomic) NSString *textURL;
@property (strong, nonatomic) NSString *title;
+ (NSDictionary*)getAttributes;
@end
@implementation RKBanner
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"idNum", @"name", @"addressURL", @"bannerPosition", @"imageALT", @"imageHeight",
@"imageURL", @"imageWidth", @"subtitle", @"targetURL", @"textURL", @"title"]
forKeys:@[@"ID", @"Name", @"AddressURL", @"BannerPosition", @"ImageALT", @"ImageHeight",
@"ImageURL", @"ImageWidth", @"SubTitle", @"TargetURL", @"TextURL", @"Title"]];
}
@end
@interface RKBranches : NSObject
@property (strong, nonatomic) NSNumber *idNum;
@property (strong, nonatomic) NSString *branchTitle;
@property (strong, nonatomic) NSString *address;
@property (strong, nonatomic) NSNumber *branchType;
@property (strong, nonatomic) NSString *city;
@property (strong, nonatomic) NSString *fax;
@property (assign, nonatomic) BOOL isCanOrder;
@property (assign, nonatomic) BOOL isMe;
@property (strong, nonatomic) NSString *openHours;
@property (strong, nonatomic) NSString *orderCode;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *remarks;
+ (NSDictionary*)getAttributes;
@end
@implementation RKBranches
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"idNum", @"branchTitle", @"address", @"branchType", @"city", @"fax",
@"isCanOrder", @"isMe", @"openHours", @"orderCode", @"phone", @"remarks"]
forKeys:@[@"ID", @"Name", @"Address", @"BranchType", @"City", @"Fax",
@"IsCanOrder", @"IsMe", @"OpenHours", @"OrderCode", @"Phone", @"Remarks"]];
}
@end
@interface RKShortToken : NSObject
@property (strong, nonatomic) NSString *responseError;
@property (strong, nonatomic) NSString *shortToken;
+ (NSDictionary*)getAttributes;
@end
@implementation RKShortToken
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"responseError", @"shortToken"]
forKeys:@[@"responseError", @"shortToken"]];
}
@end
@interface RKLongToken : NSObject
@property (strong, nonatomic) NSString *responseError;
@property (strong, nonatomic) NSString *responseMessage;
@property (strong, nonatomic) NSString *responseHttpCode;
@property (strong, nonatomic) NSString *responseUserMessage;
@property (strong, nonatomic) NSString *abroadInd;
@property (strong, nonatomic) NSString *accountType;
@property (strong, nonatomic) NSString *customerID;
@property (strong, nonatomic) NSString *longToken;
+ (NSDictionary*)getAttributes;
@end
@implementation RKLongToken
+ (NSDictionary*)getAttributes
{
return [NSDictionary dictionaryWithObjects:@[@"responseError", @"responseMessage", @"responseHttpCode", @"responseUserMessage",
@"abroadInd", @"accountType", @"customerID", @"longToken"]
forKeys:@[@"responseError", @"responseError.DeveloperMessage", @"responseError.HttpCode", @"responseError.UserMessage",
@"abroadInd", @"accountType", @"customerId", @"longToken"]];
}
@end
当我尝试调用 RKShortToken 时,我得到了很好的响应,但我得到的对象是 RKLongToken:
- (void)quickLoginWithTelephone:(NSString*)telephone extraCode:(NSString *)extraCode completionBlock:(quickLoginExtraCodeCompletionBlock)success
{
NSDictionary *params = @{ @"phone" : telephone, @"extraCode" : extraCode };
[[RKObjectManager sharedManager] getObjectsAtPath:@"users/login/quick/shortToken?"
parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"%@", operation.HTTPRequestOperation.responseString);
NSLog(@"%@", mappingResult.array);
success(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Error occurred.");
}];
}
这是日志:
2014-01-06 15:46:01.428 Online[8796:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&extraCode=null&phone=0505717596'
2014-01-06 15:50:03.999 Online[8796:60b] {"responseError":null,"shortToken":"I55933325601458654742"}
2014-01-06 15:46:01.577 Online[8796:60b] (
"<RKLongToken: 0x14dd3a50>"
)
2014-01-06 15:46:04.490 Online[8796:1403] I restkit.network:RKObjectRequestOperation.m:250 GET 'http://10.174.10.35/service/service.svc/users/login/quick/shortToken?&extraCode=null&phone=0505717596' (200 OK / 1 objects) [request=3.0587s mapping=0.0030s total=3.1129s]
这里似乎有什么问题?
更新:
我已经按照@Wain 的指示将pathPattern
添加到描述符中,但现在出现错误。它看起来像这样:
"No mappable object representations were found at the key paths searched."
NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: Banners, Branches, BranchTypes, Cities, MenuList
The representation inputted to the mapper was found to contain nested object representations at the following key paths: responseError, shortToken
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}`
最佳答案
您所有的响应描述符都有 pathPattern:nil
,因此 RestKit 无法过滤并将应用所有这些描述符并查看会发生什么。因此,对于您的所有 JSON,您将获得多个对象,其中仅包含每个映射中与 JSON 匹配的部分。
要解决此问题,请添加路径模式以允许 RestKit 确定哪个响应描述符与您的每个请求匹配。
关于ios - RestKit 映射了错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20951445/
我使用 RestKit 与 REST api 进行交互。对于某些操作,例如 HTTP PUT/POST/DELETE,我只关心响应状态代码(200 或 500 等),而不关心响应数据,即使 API 确
我在客户端和服务器端都有一个这样的响应对象: 客户端: //BaseResponse.h @interface BaseResponse : NSObject{ NSString *code;
我正在尝试将 RestKit 0.20 集成到我的项目中,例如,我使用 google rss 阅读器作为示例项目。 这是对 Xml 的响应 / /类别> & 这是我必须从中获取数据的 Xml。 RKO
我很难使用 CocoaPods 将 RestKit 安装到我的项目中。我收到未找到的错误。 我的 Pods.xcconfig 看起来像这样: GCC_PREPROCESSOR_DEFINITIONS
大多数 RESTful API 都会路由对资源的访问,例如 Rabbit 对象,如下所示: GET /rabbits <--- GET all rabbits.
问题 在 iOS 上完成 Restkit 的安装说明后,我收到 Apple Mach-o 链接器错误: 我的流程 我尝试按照此处的 Restkit 安装说明进行操作:https://github.co
我完全按照此处找到的这些步骤操作: http://www.raywenderlich.com/13097/intro-to-restkit-tutorial 但是....我得到了错误: 这可能是什么原
我正在尝试使用 RestKit 调用需要基本身份验证的端点。 RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[JSNCateg
我想用RestKit ,但我已经使用 Realm.io而不是 CoreData。 是否可以在 Realm.io 之上使用 RestKit? 最佳答案 你当然可以。从 RestKit 取回对象后: //
我没有找到任何方法来设置 restkit 0.20.0 的超时间隔。 任何人都可以帮助增加超时间隔。 谢谢 最佳答案 子类 RKHTTPRequestOperation 和实现方法 - (NSURLR
我正在尝试实现 RESTKit 0.20 操作队列,我在博客上读到 NSOperationQueue 也可用于创建操作队列。我想使用 RestKit 操作队列的 native 方法。 任何人都可以发布
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 8年前关闭。 Improve thi
我在这里做的事情很奇怪吗? 我的类别得到下载和映射, 产品也像日志记录所说的那样被下载和映射, 但是我的产品在每个分类下都是空的,谢谢! { "productsCategories" : [
我尝试了几乎所有的解决方案,但都不适合我。我在 Xcode 6 上收到此错误。我也更新了 cocoa pod ,也更新了 pod 文件。 安装更新后,我在下面添加了几行: [!] From now o
我正在尝试使用 Jenkins-CI 自动构建一个使用 Restkit 的 iOS 项目,显然,restkit 作为一个项目驻留在主项目中。该项目使用 XCode-IDE 成功构建。 但是当我使用 x
我在 Xcode 6.1 中开发项目。它工作正常,突然提示错误“未找到 Restkit/Restkit.h 文件”。从 $ 更改Architectures 后出现错误(ARCHS_STANDARD_3
我按照 github 上的说明进行操作页面但有问题(肯定是我:P)但是,您知道在 Xcode4 RestKit 中集成/使用的任何好的教程吗? 谢谢! 最佳答案 从此链接下载最新版本 http://r
我在尝试让 RestKit 构建和工作时遇到了麻烦。我收到可怕的“词法或预处理器问题:未找到 RestKit/RestKit.h 文件”消息。我使用 Xcode 4.2,我的项目适用于 iOS。 早些
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:BASE_URL]]; PPPS
有没有办法使用对象管理器访问请求的成功块中的响应数据? [objectManager postObject:[User class] path:@"/users" parameters:nil suc
我是一名优秀的程序员,十分优秀!