2012-10-21 70 views
0

我試圖解析JSON使用ASIHttpRequset 我寫了這個代碼解析JSON使用ASIHttpRequest

-(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); 
} 
} 

和bookOwn.php我寫了下面

<?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(); 

} 
?> 

如果代碼是好的,這條線將被打印

from server using ASIHttpRequest 

但它不打印,我不能確定我的代碼中有什麼問題。 有什麼幫助嗎? 在此先感謝。

+1

感謝您以明文形式公開提供您的服務URL和相應的憑據。 – 2012-10-21 13:34:01

+0

另請注意,您的帖子的編輯歷史記錄也是公開的。你應該改變你的密碼。 – jackslash

回答

0

看起來您正在執行異步請求[request startAsynchronous];,然後檢查下一行以查看是否有數據。異步意味着它將在稍後執行。通常,當請求完成加載時,會成爲請求的委託來獲得通知。

更爲迫切:

不要使用ASIHTTPRequest。 It has been deprecated by its author。請注意0​​上的橫幅,建議使用別的東西

對於替代的URL框架AFnetworking是受歡迎的。

另外NSURLConnection不是那麼糟糕。

最後,如果你的目標是iOS 5或更高版本(並沒有太多理由支持更少),你不再需要SBJSON。操作系統提供了NSJSONSerialisation,用於將JSON轉換爲對象並返回。

+0

非常感謝,[request startAsynchronous];解決了我的問題。 – user1553381