2012-08-04 62 views
1

任何機構都可以幫助我, 我想從網站解析,因爲我是新的目標C,我不知道該怎麼辦,是否有任何示例代碼,我可以看到得到一些想法?從網絡解析

+0

要分析是什麼? Html/xml或二進制數據?你想從流中解析它,還是想先下載它? – Tutankhamen 2012-08-04 15:38:48

+0

我想分析視頻的鏈接,並解析新聞的鏈接。然後在表格中顯示視頻的鏈接,並在不同的表格中顯示新聞的鏈接。 – Hamid 2012-08-04 16:28:42

+0

在這種情況下,你最好使用ASIHTTPRequest庫...(見下文)。 – Tutankhamen 2012-08-04 17:02:12

回答

1

試試這個:http://allseeing-i.com/ASIHTTPRequest/

- (IBAction)grabURLInBackground:(id)sender 
{ 
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 

    // Use when fetching binary data 
    NSData *responseData = [request responseData]; 
} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSError *error = [request error]; 
} 
+0

ASI很好,但不再支持。嘗試MBRequest! https://github.com/mobiata/MBRequest – logancautrell 2012-08-05 17:39:36

+0

我不知道爲什麼我的問題投下來!我不能再問一個問題:( – Hamid 2012-08-18 04:31:00

0

你的問題是非常不明確的,因爲該方法取決於你想要解析哪種數據。

如果你只是想填補你的網站的內容的字符串,你可以使用:如果你想解析XML或JSON文件有喜歡的NSXMLParser類

NSString *foo = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.com"] encoding:NSASCIIStringEncoding error:nil]; 

+0

切勿從主線程調用該方法。它會阻止並可能殺死你的應用程序。 – logancautrell 2012-08-04 15:27:02

+0

我想解析的網站有視頻鏈接和新聞鏈接,我想解析這些鏈接 – Hamid 2012-08-04 15:39:44

+0

由於@logancautrell表示您需要第二個線程來執行此操作。 '[self performSelectorInBackground:@selector(readThread)withObject:[NSThread currentThread]];'作爲下一步你可能會閱讀這篇文章[鏈接](http://stackoverflow.com/questions/3020849/simple-libxml2-html- parsing-example-using-objective-c-xcode-and-htmlparser-h)關於Xcode中的html解析的一些信息 – ChoboDev 2012-08-04 16:43:49