当我试图在post方法NSUrlSessionTask中发送数据时,我收到此错误?

时间:2016-02-08 06:47:36

标签: ios objective-c nsurlsession nsurlsessiontask

错误:

  

错误域= 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];

将post string转换为nsdata for 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方法都可以正常工作,除非这是错误的吗?

0 个答案:

没有答案