2013-03-25 120 views
1

我從服務器JSON解析數據中的iPhone應用程序在跟隨它的使用需要任意搜索文本字段,並張貼然後服務器文字和returs數據 匹配下面是我的iphone代碼解析JSON數據

NSString*searchText=searchTextField.text; 

NSString *post =[[NSString alloc] initWithFormat:@"searchCode=%@",searchText]; 

NSURL *url=[NSURL URLWithString:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/searchCatalog.php?"]; 

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
[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]; 


    NSError *error; 
    NSURLResponse *response; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 



    NSData* myData=[data dataUsingEncoding:NSUTF8StringEncoding]; 

    NSString *json_string = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; 
    NSArray *dataArr=[json_string JSONValue]; 

    for (int i=0; i<[dataArr count]; i++) { 


     if (!dataArr || !dataArr.count){ 


      if(resultArray!=nil){ 
       resultArray=nil; 
       resultArray=[[NSMutableArray alloc]init]; 
      } 




     } 



     NSDictionary *dict=[dataArr objectAtIndex:i]; 

     ObjectData *theObject =[[ObjectData alloc] init]; 



     [theObject setCategory:[dict objectForKey:@"category"]]; 
     [theObject setSub_Category:[dict objectForKey:@"sub_Category"]];  
     [theObject setContent_Type:[dict objectForKey:@"content_Type"]]; 
     [theObject setContent_Title:[dict objectForKey:@"content_Title"]]; 
     [theObject setPublisher:[dict objectForKey:@"publisher"]]; 
     [theObject setContent_Description:[dict objectForKey:@"content_Description"]]; 
     [theObject setContent_ID:[dict objectForKey:@"content_ID"]]; 
     [theObject setContent_Source:[dict objectForKey:@"content_Source"]]; 





     [resultArray addObject:theObject]; 
     [theObject release]; 
     theObject=nil; 



    NSLog(@"%@", json_string); 

這裏是JSON字符串的結果

 ProductivoApp[2087:c203] -JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x57b5a10 {NSLocalizedDescription=Unrecognised leading character} 

我對URL

PHP代碼
 $flu=$_POST['searchCode']; 


     $query =mysql_query("SELECT * From catalog_Master WHERE category_Title LIKE '%$flu%'"); 

    $rows = array(); 
    while($row = mysql_fetch_assoc($query)) { 
    $rows[] = $row; 
    } 
+0

你是否檢查過瀏覽器中的服務URL是否正確返回響應數據? – Ganapathy 2013-03-25 08:42:15

+0

爲什麼你有一個「?」在你的URL結尾處,在POST中你不需要它。希望這有助於 – jAmi 2013-03-25 08:47:38

+0

@Ganapathy它顯示 警告:mysql_fetch_assoc():提供的參數不是在/home/content/i/h/u/ihus235/html/cs/pah_brd_v1/productivo/searchCatalog.php中的有效MySQL結果資源在線68 [] – 2013-03-25 08:49:59

回答

0

根據您的最新評論,原因是您的查詢失敗。

首先,你不應該使用mysql_*函數。看到大紅色框here。考慮使用PDOMySQLi代替。其次,看起來你可能會打開SQL injection。你應該是escaping your queries

第三,您應該對您的查詢執行錯誤檢查。類似的東西:

if(!$query) { 
    die('Query failed. ' . mysql_error()'); 
} 

這應該給你一個想法,爲什麼查詢失敗。

你還沒有發佈你的代碼mysql_connect(),你也應該錯誤檢查這一點。類似於:

$link = mysql_connect('localhost', 'user', 'pass'); 
if(!$link) { 
    // Handle it 
}