0

I have a few web services and I want to get the JSON code and put in an array.

This is my code:

NSArray * mnewarray;
@try {
    mnewarray = [self JsonToArray:[NSString stringWithFormat:@"%@/WS_Get_APEM_Staff.php",_GV.WSpath]];

}
@catch ( NSException *e ) {
}

and

- (NSArray *) JsonToArray:(NSString *)mURLString{
    NSURL *url = [NSURL URLWithString:[mURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSError *jsonParsingError = nil;

    NSArray *result = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:&jsonParsingError];

    NSLog(@"Result= %@",result);

    return result;
}

Here is my webservice

<?php

 $error    = ["error" => "1"];

  include "_Connect.php";

     $tsql = "SELECT *
     FROM staff";

     $stmt = sqlsrv_query( $conn, $tsql);

     if($stmt)
     {
      $a=0;
          echo "[";
           while($object = sqlsrv_fetch_object($stmt)) { if($a==1) echo ","; echo json_encode($object); $a=1; }
          echo "]";
          }else
              echo json_encode($error);


         include "_Disconnect.php";

           ?>

and this is what its post the Json

[{"employeeusername":"123","employeepassword":"123","employeename":"nnnnnnnnnnnn","employeeactive":1,"trabajador":null,"id_staff":1}


  {"employeeusername":"456","employeepassword":"456","employeename":"oooooooooo","employeeactive":1,"trabajador":null,"id_staff":2},

   {"employeeusername":"789","employeepassword":"789","employeename":"yyyyyyyyyyy","employeeactive":1,"trabajador":null,"id_staff":3},

       {"employeeusername":"ewer","employeepassword":"ewer","employeename":"xxxxxxx","employeeactive":1,"trabajador":null,"id_staff":4},

     {"employeeusername":"asdfg","employeepassword":"asdfg","employeename":"asdfghj","employeeactive":1,"trabajador":null,"id_staff":5}]

The problem is the result return empty then the code doesn't run and makes an exception. Please help me with this.

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
  • You can post the Json that you server send? This question is already here at https://stackoverflow.com/questions/12582166/how-to-display-an-json-array-in-xcode?rq=1 – Reinier Melian May 26 '16 at 22:00
  • ok edit the post. 1 min – Samuel Ibarra May 26 '16 at 22:10
  • ready i post the request – Samuel Ibarra May 26 '16 at 22:17
  • You can post the exception? Anyway I see that you use NSString *string = dic[@"array"]; and don't see something like array in your server response – Reinier Melian May 26 '16 at 22:24
  • @"NSInvalidArgumentException" - reason: @"data parameter is nil" the problem is in NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSError *jsonParsingError = nil; NSLog(@"Result= %@",response); NSArray *result = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:&jsonParsingError]; response and result don't return noting its in null – Samuel Ibarra May 26 '16 at 22:36
  • You need to pass an `&error` to `+[NSURLConnection sendSynchronousRequest:returningResponse:error:]` and print it. This is likely an App Transport Security issue – Max Chuquimia May 26 '16 at 23:26
  • thank you , and yes its a App Transport Security, i solve the problem with this http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http thank you to both – Samuel Ibarra May 27 '16 at 21:24

1 Answers1

0

i finally find the error, the response returns nil for a NSAppTransportSecurity so... i only try this Transport security has blocked a cleartext HTTP

and the web services works

Community
  • 1
  • 1