I’m trying to parse JSON Using ASIHttpRequset
I wrote this code
-(void) tryASIHttpRequest{
NSString *phpUrl = @"http://www.myURL.com/subfolder/myFile.php";
NSString *dbName = @"dbName";
NSString *localHost = @"localhost";
NSString *dbUser = @"dbUser";
NSString *dbPwd = @"password";
NSString *S_user_id = [NSString stringWithFormat:@"%d",u_id0];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setRequestMethod:@"POST"];
[request setPostValue:dbName forKey:@"dbName"];
[request setPostValue:localHost forKey:@"localHost"];
[request setPostValue:dbUser forKey:@"dbUser"];
[request setPostValue:dbPwd forKey:@"dbPwd"];
[request setPostValue:S_user_id forKey:@"user_id"];
[request setPostValue:@"" forKey:@"submit"];
[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request error];
[request startAsynchronous];
if (!error) {
NSData *response = [request responseData];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
for (NSDictionary *status in statuses)
{
NSString *bo_id2 = [status objectForKey:@"bo_id"];
NSString *bo_name2 = [status objectForKey:@"bo_name"];
NSLog(@"from server using ASIHttpRequest");
NSLog(@"bo_id: %@ - bo_name: %@", bo_id2, bo_name2);
}
}else{
NSLog(@"ASIHttp Error: %@", error);
}
}
and in bookOwn.php I wrote the following
<?php
if (isset($_POST['submit'])) {
$dbName = $_POST['dbName'];
$localHost = $_POST['localHost'];
$dbUser = $_POST['dbUser'];
$dbPwd = $_POST['dbPwd'];
$user_id = $_POST['user_id'];
$con = mysql_connect($localHost,$dbUser,$dbPwd);
$db_found = mysql_select_db("iktab_book");
mysql_query('SET CHARACTER SET UTF8');
mysql_query("SET NAMES utf8; ");
$check = mysql_query("SELECT * FROM d_book where bo_id IN (Select Distinct(sal_bo_id) From d_sales Where sal_user_id =" . $user_id . ")");
while($row=mysql_fetch_assoc($check))
$output[]=$row;
$json_encode =json_encode($output);
$utf8_decode = utf8_decode($json_encode);
echo $json_encode;
mb_convert_encoding($json_encode, 'UTF-8');
$html_entity_decode = html_entity_decode($json_encode);
mysql_close();
}
?>
if the code is ok, this line will be printed
from server using ASIHttpRequest
but it doesn’t print and I can’t determine what is the wrong in my code.
Any help ?
Thanks in Advance.
It looks like you are doing an asynchronous request
[request startAsynchronous];and then are checking on the next line to see if there is data. Asynchronous means that it will be executed later on. Usually one would become the request’s delegate to get notified when the request was finished loading.More Pressing:
Don’t use ASIHTTPRequest. It has been deprecated by its author. Note the banner on the website advising using something else
For alternative URL frameworks AFnetworking is popular.
Also NSURLConnection isn’t that bad.
And finally if you are targeting iOS 5 or higher (and theres not much reason to support less) you no longer need SBJSON.
NSJSONSerialisationis provided by the OS for converting JSON into objects and back again.