2012-04-23 86 views
0

我想在我的項目中使用TFHpple,這裏是該代碼......它相當簡單的理解:TFHpple null返回響應

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    NSURL *url = [NSURL URLWithString:@"http://v1.myurl.com/somemethod"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

- (void)viewDidUnload 
{ 
    [self setGpMapView:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

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

    // Use when fetching binary data 
    NSData *responseData = [request responseData]; 
    TFHpple *doc = [[TFHpple alloc] initWithXMLData:responseData]; 
    NSArray *elements = [doc searchWithXPathQuery:@"/entry"]; 
    NSLog(@"count: %@",[elements count]); 

} 

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

我之間也是用asihttp庫提http請求調用。 我在控制檯中得到空,我已經檢查了xml ..我知道那個標籤是肯定存在的。 請問有什麼方法嗎?

編輯 - 1

這裏是XML模式:

<feed xmlns:s="http://syndication.nhschoices.nhs.uk/services" xmlns="http://www.w3.org/2005/Atom"> 
<title type="text"> 
NHS Choices - GP Practices Near Location - richmond - Within 50km 
</title> 
<id> 
http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location?range=50&northing=174900&easting=517900 
</id> 
<rights type="text">© Crown Copyright 2009</rights> 
<updated>2012-04-24T11:54:04+01:00</updated> 
<category term="Search"/> 
<logo> 
http://www.nhs.uk/nhscwebservices/documents/logo1.jpg 
</logo> 
<author> 
<name>NHS Choices</name> 
<uri>http://www.nhs.uk</uri> 
<email>[email protected]</email> 
</author> 
<link rel="self" type="application/xml" title="NHS Choices - GP Practices Near Location - richmond - Within 50km" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location?apikey=YAHUGHET&range=50&northing=174900&easting=517900&name=richmond"/> 
<link rel="first" type="application/xml" title="first" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location?apikey=YAHUGHET&range=50&northing=174900&easting=517900&name=richmond&page=1"/> 
<link rel="next" type="application/xml" title="next" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location?apikey=YAHUGHET&range=50&northing=174900&easting=517900&name=richmond&page=2"/> 
<link rel="last" type="application/xml" title="last" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/location?apikey=YAHUGHET&range=50&northing=174900&easting=517900&name=richmond&page=10"/> 
<link rel="alternate" title="NHS Choices - Find and choose services - GP Practices" href="http://www.nhs.uk/ServiceDirectories/pages/ServiceSearch.aspx?ServiceType=GP"/> 
<s:SearchCoords>174900,517900</s:SearchCoords> 
<entry> 
<id> 
http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/32248 
</id> 
<title type="text">Sayer A</title> 
<updated>2012-04-24T10:54:04Z</updated> 
<link rel="self" title="Sayer A" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/32248?apikey=YAHUGHET"/> 
<link rel="alternate" title="Sayer A" href="http://www.nhs.uk/ServiceDirectories/Pages/GP.aspx?pid=15F37AEF-EC5B-4A90-9E72-269ADEB39629"/> 
<content type="application/xml"> 
<s:organisationSummary> 
<s:name>Sayer A</s:name> 
<s:address> 
<s:addressLine>The Richmond Green Medical Cen</s:addressLine> 
<s:addressLine>19 The Green</s:addressLine> 
<s:addressLine>Richmond</s:addressLine> 
<s:addressLine>Surrey</s:addressLine> 
<s:postcode>TW91PX</s:postcode> 
</s:address> 
<s:contact type="General"> 
<s:telephone>020 83327515</s:telephone> 
</s:contact> 
<s:geographicCoordinates> 
<s:northing>174900</s:northing> 
<s:easting>517800</s:easting> 
<s:longitude>-0.305512266736965</s:longitude> 
<s:latitude>51.4608026642546</s:latitude> 
</s:geographicCoordinates> 
<s:Distance>0.1</s:Distance> 
</s:organisationSummary> 
</content> 
</entry> 
<entry> 
+1

是否可以發佈xml模式的樣本? – Alladinian 2012-04-24 12:21:04

+0

@Alladinian我編輯了我的問題。 – Maverick 2012-04-24 12:27:02

回答

0

確定。顯然你的Root元素是<feed>,裏面有一些<entry>元素。所以你必須改變這一行(注意雙「/」)才能捕捉到它們。如果你只是記錄你的響應,以確保你正在給解析器提供正確的xml,這將會很有幫助:

NSArray *elements = [doc searchWithXPathQuery:@"//entry"]; 
+0

仍然爲空。我以前也嘗試過,但仍然給它空。 – Maverick 2012-04-24 12:41:06

+0

嘗試NSLog你的'responseString'來驗證你是否得到了有效的響應。 – Alladinian 2012-04-24 12:59:39

+0

mate..responseString即將到來...我一遍又一遍地檢查它。我可以向你保證它即將到來。 – Maverick 2012-04-24 13:03:05