菜鸟教程小白 发表于 2022-12-11 18:58:30

ios - 使用restkit api objective-c 调用Web服务时出错


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

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

<blockquote>
<p>Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 &#34;No mappable
object representations were found at the key paths searched.&#34;
UserInfo={NSLocalizedDescription=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: user The representation inputted to the
mapper was found to contain nested object representations at the
following key paths: message, success This likely indicates that you
have misconfigured the key paths for your mappings., keyPath=null,
DetailedErrors=( )}</p>
</blockquote>

<p>还有调用网络服务的方法</p>

<pre><code>;

      ;

      RKObjectMapping *responseMapping = ]; //create response and request mapping

      [responseMapping addAttributeMappingsFromDictionary:@{@&#34;phone&#34;: @&#34;phone&#34;,
                                                            @&#34;device_type&#34;: @&#34;device_type&#34;,
                                                            @&#34;device_token&#34;: @&#34;device_token&#34;,
                                                            @&#34;type&#34;: @&#34;type&#34;,
                                                            @&#34;email&#34;: @&#34;email&#34;,
                                                            @&#34;identity&#34;: @&#34;identity&#34;,
                                                            @&#34;date&#34;: @&#34;date&#34;,
                                                            @&#34;status&#34;: @&#34;status&#34;,
                                                            @&#34;name&#34;: @&#34;name&#34;,
                                                            @&#34;activation&#34;: @&#34;activation&#34;,
                                                            @&#34;image&#34;: @&#34;image&#34;,
                                                            @&#34;id&#34;: @&#34;id&#34;
                                                            }];

      RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                                method:RKRequestMethodPOST
                                                                                           pathPattern:@&#34;AgentRegister&#34;
                                                                                             keyPath:@&#34;user&#34;
                                                                                           statusCodes:];

      ;
      delegate.objectManager.requestSerializationMIMEType =RKMIMETypeFormURLEncoded;


      ;

      ;

      NSString *fcmToken = .token;

      SignUpRequest *signUpRequest = [init];
      signUpRequest.phone = txtPhoneNumber.text;
      signUpRequest.email = txtEmail.text;
      signUpRequest.identity=txtIdOrCity.text;
      signUpRequest.device_type=@&#34;IOS&#34;;
      signUpRequest.device_token=fcmToken;
      signUpRequest.type=@&#34;1&#34;;


      UIImage *image = ;


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

      RKObjectRequestOperation *operation = [ objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){

            ;
            if(mappingResult.array.count !=0){
                ;
            }else{

            }

            ;
      }failure:^(RKObjectRequestOperation *operation, NSError *error){
            ;
            NSLog(@&#34;%@&#34;,error.description);
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@&#34;..&#34;
                                                                           message:@&#34;حدث خطاء ما .. حاول مرة اخري&#34; preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@&#34;Ok&#34; style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            ;
            ;
            ;

      }];
      [ enqueueObjectRequestOperation:operation];

    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>此错误告诉您 <code>user</code> 路径中没有可用的内容。例如,如果您的响应是 JSON,则没有名为 <code>user</code> 的根键,只有 <code>message</code> 和 <code>success</code>:</p>

<p><code>{
"user": "<- 这个键不存在",
"message": "<- 这里有东西",
"success": "<- 这里也有东西"
}</code></p>

<p>您很可能需要将 <code>keyPath</code> 从 <code>user</code> 更改为 <code>success.user</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用restkit api objective-c 调用Web服务时出错,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46686658/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46686658/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用restkit api objective-c 调用Web服务时出错