我需要在两个搜索引擎中实现同时搜索。我有两个功能: *- (void) searchYoutube: (NSString )text 和 *- (void) searchYahoo:(NSString )text
(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
仅此而已。我无法接收任何数据。我做错了什么?
最佳答案
嗯,
NSURLConnection *connection = [NSURLConnection
sendSynchronousRequest:request returningResponse:nil error:nil];
错了。 sendSynchronousRequest
返回带有响应数据的 (NSData *)
。所以,做你想做的事情的更正确的方法是
NSData *responseData = [NSURLConnection
sendSynchronousRequest:request returningResponse:nil error:nil];
然后您读取来自 responseData
的响应。
您还应该查看 NSURLConnection 的文档.
关于objective-c - NSURLConnection sendSynchronousRequest 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482994/