开发者

Problem with NSURLConnection sendSynchronousRequest

I need to i开发者_JAVA百科mplement a simultaneous search in two search engines. I have two functions: *- (void) searchYoutube: (NSString )text and *- (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);
}}

Function -searchYoutube: is similar to the above. But when I call query with these two functions, I see

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

And that's all. I can't receive any data. What am I doing wrong?


Well,

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

Is wrong. sendSynchronousRequest returns an (NSData *) with the response data. So, a more correct way to do what you want is

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

And then you read the response from responseData.

You should also take a look at the documentation for NSURLConnection.


This is not an answer, but a suggestion to enhance performance, Answer is already given by uvesten.

sendSynchronousRequest is not a good way to implement, it will not leave control until request is completed, so if it takes time to load, app will not respond to touches.

Instead, you can try

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];

and implement delegate methods

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜