使用Restkit v0.20进行Json映射

时间:2013-03-18 10:23:56

标签: ios xcode4.5 restkit

我能够正确发送请求,但收到回复后我

response.body ={"status":"success","loginToken":"xxxxyyyzzz"}

但错误说

 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

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

}];
}

我认为上面使用的代码是用数组映射响应, 所以我需要代码来映射简单的json像这样一个response.body = {“status”:“success”,“loginToken”:“logingngngngngngngae”}

1 个答案:

答案 0 :(得分:0)

使用https://github.com/RestKit/RestKit/wiki/Object-mapping中提到的pathpattern,non keyvalue映射来映射此类响应。

我使用的是带有“?”的pathPattern在它,因此它没有正确匹配。 ,路径模式也不应该在开头有“/”。 如果我将pathPattern更改为pathPattern,上面的代码工作正常:@“site / login” 和postObject到postObject:@“site / login” 并且当然改变了baseurl = @“http://json.xxxxxxx.xxxxx:8179/jsonresponse/index.php”,因此其中没有特殊的字符。

相关问题