2014-02-12 59 views
0

我有一個應用程序接收由我的jason.php腳本生成的JSON文件,並在表視圖中顯示數據。ios json不工作時使用include()

它工作正常,直到我嘗試在我的jason.php文件中使用'include(db_connect.php)'將數據庫日誌詳細傳遞給它。

運行我的PHP腳本,以「包括(db_connect.php)」,確實工作在瀏覽器中(返回正確格式化的JSON文件),但它確實我的手機上沒有工作。

但是..

它的工作我的手機上,如果我只是db_connect.php的內容粘貼到jason.php文件......它在瀏覽器返回完全相同的JSON文件。

兩種方式在瀏覽器中返回完全相同的JSON文本。

所有應用程序都期望從指定的URL接收JSON文件,但不會傳遞任何內容。只需訪問URL並存儲NSData對象中返回的內容。

如果有人知道爲什麼會發生這種情況,我將不勝感激!

感謝

jason.php:這在我的瀏覽器完全返回的JSON腳本

<?php 

require("db_connect.php"); 


//Check to see if we can connect to the server 
if(!$connection) 
{ 
    die("Database server connection failed."); 
} 
else 
{ 
    //Attempt to select the database 
    $dbconnect = mysql_select_db($db, $connection); 

    //Check to see if we could select the database 
    if(!$dbconnect) 
    { 
     die("Unable to connect to the specified database!"); 
    } 
    else 
    { 
     $query = "SELECT * FROM cities"; 
     $resultset = mysql_query($query, $connection); 

     $records = array(); 

     //Loop through all our records and add them to our array 
     while($r = mysql_fetch_assoc($resultset)) 
     { 
      $records[] = $r;   
     } 

     //Output the data as JSON 
     echo json_encode($records); 
    } 


} 


?> 

db_connect.php日誌中的細節

<?php 
    $host = "xxxxx"; //Your database host server 
    $db = "xxxxx"; //Your database name 
    $user = "xxxxx"; //Your database user 
    $pass = "xxxxx"; //Your password 
    $connection = mysql_connect($host, $user, $pass); 
?> 

jason_pasted.php這是完全一樣的,但jason.php的db_connect.php的內容只是在粘貼 - 產生完全相同的結果在瀏覽器中,並在我的應用程序一起使用時。 DR應用程序完美的作品與jason_pasted.php但不jason.php;從應用程序代碼

-(void) retrieveData 
{ 
    NSURL *url = [NSURL URLWithString:jsonURL]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 
    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

    //set up cities array 
    citiesArray = [[NSMutableArray alloc]init]; 

    for (int i=0;i<[json count]; i++) 
    { 
     //create city object 
     NSString *cID = [[json objectAtIndex:i] objectForKey:@"id"]; 
     NSString *cName = [[json objectAtIndex:i] objectForKey:@"cityName"]; 
     NSString *cState = [[json objectAtIndex:i] objectForKey:@"cityState"]; 
     NSString *cPopulation = [[json objectAtIndex:i] objectForKey:@"cityPopulation"]; 
     NSString *cCountry = [[json objectAtIndex:i] objectForKey:@"country"]; 

     City *myCity = [[City alloc] initWithCityID:cID 
             andCityName:cName 
             andCityState:cState 
            andCityPopulation:cPopulation 
            andCityCountry:cCountry]; 

     //add city oject to city array 
     [citiesArray addObject:myCity]; 

    } 

    [davesTableView reloadData]; 


} 

TL

<?php 

$host = "xxxxx"; //Your database host server 
$db = "xxxxxx"; //Your database name 
$user = "xxxxx"; //Your database user 
$pass = "xxxxxx"; //Your password 

$connection = mysql_connect($host, $user, $pass); 


//Check to see if we can connect to the server 
if(!$connection) 
{ 
    die("Database server connection failed."); 
} 
else 
{ 
    //Attempt to select the database 
    $dbconnect = mysql_select_db($db, $connection); 

    //Check to see if we could select the database 
    if(!$dbconnect) 
    { 
     die("Unable to connect to the specified database!"); 
    } 
    else 
    { 
     $query = "SELECT * FROM cities"; 
     $resultset = mysql_query($query, $connection); 

     $records = array(); 

     //Loop through all our records and add them to our array 
     while($r = mysql_fetch_assoc($resultset)) 
     { 
      $records[] = $r;   
     } 

     //Output the data as JSON 
     echo json_encode($records); 
    } 


} 


?> 

ViewController.m提取物。 jason.php和jason_pasted.php在瀏覽器中打開時返回完全相同的JSON腳本。


字符串從jason.php和jason_pasted.php返回

(
    { 
    cityName = London; 
    cityPopulation = 8173194; 
    cityState = London; 
    country = "United Kingdom"; 
    id = 1; 
}, 
    { 
    cityName = Bombay; 
    cityPopulation = 12478447; 
    cityState = Maharashtra; 
    country = India; 
    id = 2; 
}, 
    { 
    cityName = "Kuala Lumpur"; 
    cityPopulation = 1627172; 
    cityState = "Federal Territory"; 
    country = Malaysia; 
    id = 3; 
}, 
    { 
    cityName = "New York"; 
    cityPopulation = 8336697; 
    cityState = "New York"; 
    country = "United States"; 
    id = 4; 
}, 
    { 
    cityName = Berlin; 
    cityPopulation = 3538652; 
    cityState = Berlin; 
    country = Deutschland; 
    id = 5; 
} 
) 

返回錯誤,只有當NSURL點傑森。PHP

2014-02-13 11:43:34.760 JSONios[4655:60b] JSON error: Error Domain=NSCocoaErrorDomain Code=3840 
"The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or 
object and option to allow fragments not set.) 
UserInfo=0x14659c40 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 

回答

0

這是擺在回答格式化:

不要忽略錯誤!

錯誤:

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

注:文檔不指定爲零,可以通過爲error參數。

正確:

// There is an assumption that the JSON head is a dictionary. 
NSError *error; 
NSDictionary *jsonAsDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
if (jsonAsDict == nil) { 
    NSLog(@"JSON error: %@", error); 
} 
else { // For debug only 
    NSLog(@"JSON: %@", jsonAsDict); 
} 

現在,有了這個代碼,會發生什麼?
如果可能,還請提供JSON字符串。
哦,我個人不在乎php如何創建JSON,我只需要看到的就是JSON。

還是有問題:NSLog的數據作爲一個字符串:

NSLog(@"data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 

如果沒有數據來代替dataWithContentsOfURL:

+0

嗨Zaph的添加錯誤參數 dataWithContentsOfURL:options:error:
,非常感謝您的輸入。我添加了從jason.php和jason_pasted.php輸出的字符串。它們都是相同的。當NSUrl指向jason_pasted.php時,我的應用程序運行良好,並從「NSLog(@」JSON:%@「,jsonAsDict)打印出來時返回與瀏覽器相同的JSON腳本;'我還添加了當NSUrl指向jason.php(誰的唯一區別是使用''include(db_connect.php)''傳入服務器日誌詳細信息) –