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

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

}];
}

上記のコードは response を array にマップするためのものだと思うので、このような単純な json をマップするコードが必要です。

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.xxxxxxxx.xxxxx:8179を変更すると正常に動作します。 /jsonresponse/index.php " に特別な文字が含まれないようにします。

于 2013-03-21T05:12:11.200 に答える