2011-06-26 12 views
0

私は2つの検索エンジンで同時検索を実装する必要があります。 (NSStringの)テキスト* - (無効)searchYahoo:: - (無効)searchYoutube *:私は2つの機能があります(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を:あります上記と同様です。しかし、これら2つの関数でクエリを呼び出すと、私は参照してください

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は実装には適していませんが、リクエストが完了するまで制御を放棄しないため、読み込みに時間がかかるとアプリは応答しません。

代わり

、あなたは

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{ 
} 
関連する問題