Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSURLConnection sendSynchronousRequest出现问题_Objective C_Ios_Nsurlconnection - Fatal编程技术网

Objective c NSURLConnection sendSynchronousRequest出现问题

Objective c NSURLConnection sendSynchronousRequest出现问题,objective-c,ios,nsurlconnection,Objective C,Ios,Nsurlconnection,我需要在两个搜索引擎中同时执行搜索。我有两个职能: *-(无效)搜索YouTube:(NSString)文本 *-(无效)搜索雅虎:(NSString)文本 函数-searchYoutube:与上述类似。但当我用这两个函数调用query时,我看到 2011-06-26 14:06:27.523 TrueSearcher[5373:207] URL query: http://gdata.youtube.com/feeds/api/videos?q=os&start-index=1&

我需要在两个搜索引擎中同时执行搜索。我有两个职能: *-(无效)搜索YouTube:(NSString)文本 *-(无效)搜索雅虎:(NSString)文本

函数-searchYoutube:与上述类似。但当我用这两个函数调用query时,我看到

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
读取响应


您还应该查看有关的文档

这不是一个答案,而是一个提高性能的建议,答案已经由uvesten给出了

sendSynchronousRequest不是一个很好的实现方式,它在请求完成之前不会离开控制,所以若需要时间加载,应用程序将不会响应触摸

相反,你可以试试看

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{
}
- (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{
}