I trying to make an app and a web service using iOS, php/mySQL/json. Ive got it working so far, the app reads a php file that simply reads a table in the db so far. No variables used in the query. Then it encodes the data json type and the app reads it. The blue code works fine, the red one doesnt
The iOSapp code is:
+ (NSArray)fetchInfoForUDID: (NSString )udid{
NSString urlString = [NSString stringWithFormat:@"http://www.myserver.com/myapp/getusers.php"];
NSURL url = [NSURL URLWithString:urlString];
return [self fetchJSONValueForURL:url];
}
- (id)fetchJSONValueForURL: (NSURL )url{
NSString jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
id jsonValue = [jsonString JSONValue];
[jsonString release];
return jsonValue;
}[/B]
btw, im not using the udid variable in this above section...
Here is the web service php code:
getusers.php
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "usr", "pas") or die("Could not connect");
mysql_select_db("db") or die("Could not select database");
$query = "SELECT FROM users";
$result = mysql_query($query);
$arr = array();
$rs = mysql_query("SELECT FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
This works fine. I get the json encoded array and present it in a tableVC.[/COLOR]
But when i try to pass a variable to get specific values from the db is where im stumped. The php part works fine because ive made an html form to test it and i get the correctly formatted array. Its the iOS-php part that is not clear to me.
Here is the iOSapp code:
+ (NSArray )fetchTimelineForUDID: (NSString )udid{
NSString urlString = [NSString stringWithFormat:@"http://www.myserver.com/myapp/readtags2.php?userudid=%@", udid];
NSURL url = [NSURL URLWithString:urlString];
return [self fetchJSONValueForURL:url];
}
- (id)fetchJSONValueForURL: (NSURL )url{
NSString jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
id jsonValue = [jsonString JSONValue];
[jsonString release];
return jsonValue;
}[/B]
and here is the php code:
readtags2.php
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "usr", "pas") or die("Could not connect");
mysql_select_db("db") or die("Could not select database");
$userudid = $_POST["originudid"];
$arr = array();
$rs = mysql_query("SELECT * FROM tags WHERE originudid='$userudid' ");
while($obj = mysql_fetch_object($rs))
{
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
[/COLOR]
like i said, i know the php is fine because using a regular html form to post to it, it returns the correctly formatted array.
So then i tried logging the jsonString in + (id)fetchJSONValueForURL and noticed it was coming back [url]http://www......readtags2.php[/url] without the ?udid=bla bla bla... So i remembered that would be a GET method and im using a POST method in the php, or maybe the json encoding of the jsonString doesnt allow for ?var=blablabla....so i tried doing this:
+ (NSArray )fetchTimelineForUDID: (NSString )udid{
NSData postData = [udid dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/myapp/readtags2.php"]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse response;
NSError error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
return (error == nil); //<------this is my question
}[/COLOR][/B]
but i get EXC BAD ACCESS. I dont know how to return the data gotten from the NSURLRequest. Im reading the Class reference but i dont quite get it...any tips?