0

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

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."}

登录响应.h

@interface LoginResponse : NSObject

@property (nonatomic, strong) NSString *status;

@property (nonatomic, strong) NSString *loginToken;

+(RKObjectMapping*)defineLoginResponseMapping;

@end

登录响应.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":"logingngngnnggngngae"}

4

1 回答 1

0

此类响应将使用https://github.com/RestKit/RestKit/wiki/Object-mapping中提到的 pathpattern 非键值映射进行映射 。

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

于 2013-03-21T05:12:11.200 回答