2011-06-26 39 views
0

我需要在兩個搜索引擎中實現同步搜索。我有兩個功能: * - (空)searchYoutube:(NSString的)文本* - (空)searchYahoo:(NSString的)文本NSURLConnection的問題sendSynchronousRequest

(void) searchYahoo:(NSString *)text{ 

    NSString *urlString = [NSString stringWithFormat:@"http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=%@&output=json&query=%@&results=30", YahooAppId, text]; 
NSLog(@"URL zaprosa: %@",urlString); 

//Create NSURL string from formatted string 
NSURL *url = [NSURL URLWithString:urlString]; 


NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
NSURLConnection *connection = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
if (connection) 
{ 
    NSLog(@"Data will be received from URL: %@", url); 
} 
else 
{ 
    NSLog(@"Data couldn't be received from URL: %@", url); 
}} 

功能-searchYoutube:是與上面類似。但是,當我用這兩個函數調用查詢時,我看到

2011-06-26 14:06:27.523 TrueSearcher[5373:207] URL query: http://gdata.youtube.com/feeds/api/videos?q=os&start-index=1&max-results=30&alt=jsonc&v=2 
2011-06-26 14:06:27.874 TrueSearcher[5373:207] Data will be received from URL: http://gdata.youtube.com/feeds/api/videos?q=os&start-index=1&max-results=30&alt=jsonc&v=2 
2011-06-26 14:06:27.875 TrueSearcher[5373:207] URL query: http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=jwGJkT32&output=json&query=os&results=30 
2011-06-26 14:06:29.010 TrueSearcher[5373:207] Data will be received from URL: http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=jwGJkT32&output=json&query=os&results=30 

而就是這樣。我無法收到任何數據。我究竟做錯了什麼?

回答

1

好,

NSURLConnection *connection = [NSURLConnection 
    sendSynchronousRequest:request returningResponse:nil error:nil]; 

是錯誤的。 sendSynchronousRequest返回(NSData *)與響應數據。所以,一個更正確的方法來做你想做的是

NSData *responseData = [NSURLConnection 
    sendSynchronousRequest:request returningResponse:nil error:nil]; 

然後你從responseData讀迴應。

您還應該看看NSURLConnection的文檔。

0

這不是一個答案,而是一個提高性能的建議,答案已經由uvesten提供。

sendSynchronousRequest不是一個好的實現方式,直到請求完成纔會離開控件,所以如果加載需要時間,app不會響應觸摸。

相反,你可以嘗試

NSString *urlString = [NSString stringWithFormat:@"http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=%@&output=json&query=%@&results=30", YahooAppId, text]; 
NSLog(@"URL zaprosa: %@",urlString); 

//Create NSURL string from formatted string 
NSURL *url = [NSURL URLWithString:urlString]; 


NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

和實施委託方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    expectedSize = [response expectedContentLength]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
    if (objData==nil) { 
     objData = [[NSMutableData alloc] init]; 
    } 
    [objData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
} 
相關問題