Error:
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
This is my expected json response :
{
"success": true
"mobile": 90xxxxxxxx
"message": "OTP is generated and sent successfully"
}
This is my code :
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];
converting string into nsdata for post method:
-(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];
}
My other post method and get methods are all working except this whats wrong ?
The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.
Visit the Home Page@Rob i printed in nsstring its giving like this
– Kishore Kumar Feb 08 '16 at 07:30