2014-02-18 109 views
0

我有一个UISplitView,我试图用数据库中的值填充左侧控制器。 我有这个phpcode。从页面获取数据到程序

$query = "SELECT name FROM country"; 
$result = mysql_query($query); 

while($row = mysql_fetch_assoc($result)){ 
    $json_output[] = $row; 
} 
echo json_encode($json_output); 

但是,当我进入我的ios应用程序。我有这样的代码在我viewDidLoad

NSURL *myURL = [NSURL URLWithString:@"http://www.mywebsite.com/index.php"]; 
NSArray *test= [[NSArray alloc] initWithContentsOfURL:myURL]; 
TypesArray=[[NSMutableArray alloc]initWithContentsOfURL:test]; 

cellforRowAtIndexpat 我加

cell.textLabel.text=[TypesArray objectAtIndex:indexPath.row]; 

因此,当我跑我的节目,我希望看到在leftviewcontroller列出了我的价值观,但遗憾的是它显示了空白。NSLog也显示为空,想要一些帮助,为什么我的价值观没有被拉到我的应用程序。

感谢

+0

你能NSURL * myURL ...后插入下面的代码:NSError *错误= nil; NSString * str = [NSString stringWithContentsOfURL:myURL] encoding:NSUTF8StringEncoding error:&error]; NSLog(@“%@”,str); ? – stosha

+0

你知道如何解析json或xml吗?请学习它,并在Google中为它做一些RND。 –

+0

@NitinGohel嗯,我实际上已经在另一个应用程序中使用过这个json,我正在做它的工作。我的问题在于我的程序没有收到链接中的任何内容吗? –

回答

0

你应该使用这样的事情:

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: @"http://www.mywebsite.com/index.php"]] 
                  cachePolicy: NSURLRequestReloadIgnoringLocalCacheData 
                 timeoutInterval: 60.0]; 

NSData* responseData = [NSURLConnection sendSynchronousRequest: request 
               returningResponse: nil 
                  error: nil]; 
if (responseData) 
{ 
    NSArray* array = [[JSONDecoder decoder] parseJSONData: responseData]; 
    NSLog(@"%@", [array description]); 
} 

其中JSONDecoder您可以通过链接采取https://github.com/hollyschinsky/PGPushNotificationSample

+0

我改变了它,仍然在我的左视图控制器中空白,nslog为空,但是我的PHP链接让我回来[{“name “:”England“},{”name“:”America“},{”name“:”Phillipians“}] –

+0

我编辑了我的答案 – stosha

+0

文件夹中有很多文件,不确定要导入哪个文件以访问JSONDecoder 。另外我不明白这种方法......也不是为什么我的方法不工作 –

相关问题