objective-c - NSURLConnection sendSynchronousRequest 的问题

标签 objective-c ios nsurlconnection

我需要在两个搜索引擎中实现同时搜索。我有两个功能: *- (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/

相关文章:

ios - 带 Sinch VOIP 的 Pushkit 不能与 pushkit 一起使用

ios - 在我的绘图代码中没有调用 'dispatch_async' 的匹配函数

iphone - 如何使用 Autoresizing Mask 将 UIView 锚定在中间点之外的几个像素?

objective-c - NSMenu 阻塞计时器/全局事件监视器/等

javascript - 如何在浏览器环境中没有触摸事件的情况下获取 Activity 触摸事件

IOS模拟器安装子版本

ios - Swift:动画后将 UIView 的框架重置为其约束

iphone - 从服务器加载表格 View 的 Plist 之前检查互联网

iphone - 未访问 cachedResponseForRequest 方法

ios - NSUrlConnection sendAsynchronousRequest 和自签名证书