retrieve data from php file in Xcode

I tried different codes to both send and retrieve data from a PHP file and i'm still not able to properly get the result So far, I got the retrieval results to be displayed in the output debugger(in json format) however, not in the Xcode simulator. it seems like i'm missing something!

- (void) retrieveData

NSString * jack=[GlobalVar sharedGlobalVar].gUserName;
NSLog(@"global variable %@", jack);

NSString *rawStr = [NSString stringWithFormat:@"StudentID=%@",jack];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:@"http://m-macbook-pro.local/studentCourses.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);

  jsonArray=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

// Search for the array parameter that should be added
 coursesArray=[[NSMutableArray alloc] init];
//set up our cities array

NSString *strResult = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"me: %@", strResult);

//loop through our json array

for (int i = 0 ; i <coursesArray.count; i++)
    NSString * cName = [[coursesArray objectAtIndex:i] objectForKey:@"CourseName"];

    //Add the City object to our cities array
    [coursesArray addObject:[[Course alloc]initWithCourseName:cName]];


//Reload our table view
[self.tableView reloadData];

in php file

echo json_encode($records);


It looks like you're creating your json array with your post data, not with the responseData that comes back from the server.

//change this
jsonArray=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//to this
jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

Also, based on the code you posted it doesn't look like you ever add the results returned from the server to your coursesArray. In your loop, where you create cName, I think what you are wanting to do is grab the course name from the results from the php call (your jsonArray) and add them to your courses array. The way you have it set up you are grabbing a result from the courses array and adding it to itself.

Try this code for your for loop:

 for (int i = 0 ; i <jsonArray.count; i++)
     NSString * cName = [[jsonArray objectAtIndex:i] objectForKey:@"CourseName"];

     [coursesArray addObject:[[Course alloc]initWithCourseName:cName]];


Based on your code I'm assuming that coursesArray contains the data for your tableview.

Also be aware that it is not a good idea to use synchronous requests in a production app as they run on the main thread and will block the UI.

Need Your Help

Json Stringify date produces a wrong date compared to javascript date

javascript json stringify

When i create a javascript date and then stringify it and send it to the server, i get two different dates. The stringified date is always one day behind.

How to test login from multiple IP addresses in Rails

ruby-on-rails testing integration-testing

At the request of a customer, I had to implement sending a notification email every time the application detects two active sessions for the same user from different IP addresses. How do you test t...