使用restkit api目标c调用Web服务时出错

Posted

技术标签:

【中文标题】使用restkit api目标c调用Web服务时出错【英文标题】:error while call web service with restkit api objective c 【发布时间】:2017-10-11 11:05:56 【问题描述】:

我正在尝试调用网络服务并上传图片,

映射有问题,我花了好几个小时都没有成功。我得到的错误是:

错误域=org.restkit.RestKit.ErrorDomain 代码=1001 "不可映射 在搜索的关键路径中找到了对象表示。” UserInfo=NSLocalizedDescription=没有可映射的对象表示 在搜索的关键路径中找到。, NSLocalizedFailureReason=The 映射操作找不到任何嵌套对象表示 在搜索的关键路径处:用户输入到 发现映射器在 以下关键路径:消息、成功 这可能表明您 为您的映射配置了错误的关键路径。keyPath=null, 详细错误=( )

还有调用网络服务的方法

[SVProgressHUD show];

        [delegate.objectManager.HTTPClient.defaultHeaders setValue:@"application/x-www-form-urlencoded" forKey:@"content-type" ];

        RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[SignUpResponse class]]; //create response and request mapping

        [responseMapping addAttributeMappingsFromDictionary:@@"phone": @"phone",
                                                              @"device_type": @"device_type",
                                                              @"device_token": @"device_token",
                                                              @"type": @"type",
                                                              @"email": @"email",
                                                              @"identity": @"identity",
                                                              @"date": @"date",
                                                              @"status": @"status",
                                                              @"name": @"name",
                                                              @"activation": @"activation",
                                                              @"image": @"image",
                                                              @"id": @"id"
                                                              ];

        RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                                method:RKRequestMethodPOST
                                                                                           pathPattern:@"AgentRegister"
                                                                                               keyPath:@"user"
                                                                                           statusCodes:[NSIndexSet indexSetWithIndex:200]];

        [delegate.objectManager.defaultHeaders setValue:@"application/x-www-form-urlencoded" forKey:@"content-type" ];
        delegate.objectManager.requestSerializationMIMEType =RKMIMETypeFormURLEncoded;


        [delegate.objectManager removeResponseDescriptor:responseDescriptor];

        [delegate.objectManager addResponseDescriptor:responseDescriptor];

        NSString *fcmToken = [FIRInstanceID instanceID].token;

        SignUpRequest *signUpRequest = [[SignUpRequest alloc]init];
        signUpRequest.phone = txtPhoneNumber.text;
        signUpRequest.email = txtEmail.text;
        signUpRequest.identity=txtIdOrCity.text;
        signUpRequest.device_type=@"ios";
        signUpRequest.device_token=fcmToken;
        signUpRequest.type=@"1";


        UIImage *image = [UIImage imageNamed:@"Logo"];


        // Serialize the Article attributes then attach a file
        NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:signUpRequest method:RKRequestMethodPOST path:@"AgentRegister" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
            [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                        name:@"image"
                                    fileName:@"photo.png"
                                    mimeType:@"application/x-www-form-urlencoded"];
        ];

        RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)

            [SVProgressHUD dismiss];
            if(mappingResult.array.count !=0)
                [self performSegueWithIdentifier:@"goToVerify" sender:self];
            else

            

            [delegate.objectManager removeResponseDescriptor:responseDescriptor];
        failure:^(RKObjectRequestOperation *operation, NSError *error)
            [SVProgressHUD dismiss];
            NSLog(@"%@",error.description);
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@".."
                                                                           message:@"حدث خطاء ما .. حاول مرة اخري" preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) 

            ];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
            [delegate.objectManager removeResponseDescriptor:responseDescriptor];

        ];
        [[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation];

    

【问题讨论】:

【参考方案1】:

此错误告诉您user 路径上没有可用的内容。例如,如果您的响应是 JSON,则不会有名为 user 的根键,只有 messagesuccess

"user": "<- This key doesnt exist", "message": "<- There is something here", "success": "<- There is also something here"

您很可能需要将您的 keyPathuser 更改为 success.user

【讨论】:

以上是关于使用restkit api目标c调用Web服务时出错的主要内容,如果未能解决你的问题,请参考以下文章

服务器未从 restkit 的 web 服务调用接收正文对象

Restkit + Objective-c - 多次调用同一个 Web 服务

使用 RestKit 同时调用两个 web 服务

通用方法的同步调用 Web API 服务

与 Three20 集成的 RestKit

在目标 c 中的委托方法上设置超时