0

错误:

错误域 = NSCocoaErrorDomain 代码 = 3840 “JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。” UserInfo={NSDebugDescription=JSON 文本没有以数组或对象开头,并且允许未设置片段的选项。}

这是我预期的 json 响应:

{
"success": true
"mobile": 90xxxxxxxx
"message": "OTP is generated and sent successfully"
}

这是我的代码:

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
    [request setURL:url];
        [request setHTTPMethod:@"POST"];
        NSString *conLength=[NSString stringWithFormat:@"%lu",(unsigned long)data.length];
        [request setValue:conLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:data];

     NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                    completionHandler:
                                          ^(NSData *data, NSURLResponse *response, NSError *connectionError) {
                                              // ...
                                              NSLog(@"coming inside session side");
                                              dispatch_async(dispatch_get_main_queue(),^{

                                                  if([data length]>0 && connectionError==nil)
                                                  {
                                                      NSError *error;


                                                      NSMutableDictionary *jsonResponse=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

                                                      if(error)
                                                      {
                                                          [Utitlity alertFunction:@"Warning" message:[error localizedDescription]];
                                                          NSLog(@"%@",error);
                                                      }
                                                      else
                                                      {
                                                          [self requestComplete:jsonResponse];
                                                      }
                                                  }
                                                  else if ([data length] == 0 && connectionError == nil)
                                                  {
                                                      [Utitlity alertFunction:@"Warning" message:@"Empty Server Response"];
                                                  }
                                                  else if (connectionError != nil)
                                                  {
                                                      [Utitlity alertFunction:@"Warning" message:@"Problem in connecting to server"];
                                                  }
                                              });
                                          }];

            [task resume];

将字符串转换为 nsdata 用于 post 方法:

-(void)loginotpmobilnumber:(NSString *)mobile
{

    NSString *url=[NSString stringWithFormat:@"mobile=%@",mobile];
    NSData *dataFetch=[url dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSURL *urla=[NSURL URLWithString:[NSString stringWithFormat:@"%@",loginotp]];
    [self requestFunction:urla :dataFetch :YES];
}

我的其他 post 方法和 get 方法都在工作,除了这有什么问题吗?

4

0 回答 0