2012-02-21 3 views
0

간단한 Google 이미지 검색을 수행하고 찾고있는 문자열이 무엇이든 하나의 이미지를 반환하고 싶습니다. 이미지가 끝나면 null입니다. 내가 뭘 잘못하고 있니? 아래 코드 : 내가 개를 검색 할 때Google 이미지 검색 iPhone URL 데이터

-(void)getGoogleImage 
{ 

    NSString *google = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"string"]]; 
    NSString *newGoogle = [google stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSString *fullGoogle = [NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%@",newGoogle]; 
    NSURL * url = [NSURL URLWithString:fullGoogle]; 
    NSURLRequest * request = [NSURLRequest requestWithURL:url 
               cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; 

    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSLog(@"Did Recieve Response"); 
    responseData = [[NSMutableData alloc]init]; 
} 
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data 
{ 
    NSLog(@"Did Recieve Data"); 
    [responseData appendData:data]; 
} 
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{ 
    NSLog(@"Did Fail"); 
} 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Did Finish"); 
    image = [[UIImage alloc]initWithData:responseData]; 
} 

i는 웹 브라우저에서이 얻을 :

{"responseData": {"results":[{"GsearchResultClass":"GimageSearch","width":"220","height":"211","imageId":"ANd9GcTZKvO0mDGuDdifdhW3TEG8bmcPM23DMGEB4wevzBuIPp7HMMwQ3dTetVc","tbWidth":"107","tbHeight":"103","unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Timba%2B1.jpg/220px-Timba%2B1.jpg","url":"http://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Timba%252B1.jpg/220px-Timba%252B1.jpg","visibleUrl":"en.wikipedia.org","title":"\u003cb\u003eDog\u003c/b\u003e - Wikipedia, the free encyclopedia","titleNoFormatting":"Dog - Wikipedia, the free encyclopedia","originalContextUrl":"http://en.wikipedia.org/wiki/Dog","content":"the appropriate-sized \u003cb\u003edog\u003c/b\u003e","contentNoFormatting":"the appropriate-sized dog","tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcTZKvO0mDGuDdifdhW3TEG8bmcPM23DMGEB4wevzBuIPp7HMMwQ3dTetVc"},{"GsearchResultClass":"GimageSearch","width":"170","height":"224","imageId":"ANd9GcTycy7IfwO9VLFIftSa7yNZj1b_BP583qUQ0UmSZnog_jdhdwvewcWvrw","tbWidth":"82","tbHeight":"108","unescapedUrl":"http://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Golden_retriever_eating_pigs_foot.jpg/170px-Golden_retriever_eating_pigs_foot.jpg","url":"http://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Golden_retriever_eating_pigs_foot.jpg/170px-Golden_retriever_eating_pigs_foot.jpg","visibleUrl":"en.wikipedia.org","title":"\u003cb\u003eDog\u003c/b\u003e - Wikipedia, the free encyclopedia","titleNoFormatting":"Dog - Wikipedia, the free encyclopedia","originalContextUrl":"http://en.wikipedia.org/wiki/Dog","content":"See also: \u003cb\u003eDog\u003c/b\u003e food","contentNoFormatting":"See also: Dog food","tbUrl":"http://t1.gstatic.com/images?q\u003dtbn:ANd9GcTycy7IfwO9VLFIftSa7yNZj1b_BP583qUQ0UmSZnog_jdhdwvewcWvrw"},{"GsearchResultClass":"GimageSearch","width":"334","height":"360","imageId":"ANd9GcTsX8rcRiPIMso7akKaQmVP4PzMwoAfbVnlc6KkPNG5jpU7wfyZNWy1oNs","tbWidth":"112","tbHeight":"121","unescapedUrl":"http://www.petscarecenter.com/wp-content/uploads/2011/05/dog.jpg","url":"http://www.petscarecenter.com/wp-content/uploads/2011/05/dog.jpg","visibleUrl":"www.petscarecenter.com","title":"Tips for \u003cb\u003eDog\u003c/b\u003e Care","titleNoFormatting":"Tips for Dog Care","originalContextUrl":"http://www.petscarecenter.com/dog-care/tips-for-dog-care.html","content":"Since \u003cb\u003edogs\u003c/b\u003e are known for the","contentNoFormatting":"Since dogs are known for the","tbUrl":"http://t0.gstatic.com/images?q\u003dtbn:ANd9GcTsX8rcRiPIMso7akKaQmVP4PzMwoAfbVnlc6KkPNG5jpU7wfyZNWy1oNs"},{"GsearchResultClass":"GimageSearch","width":"400","height":"366","imageId":"ANd9GcS6VxIzGltwNb2PWMGDfoBvvuof26Kn9i_BrXYRvgWqpnnT3UNfy5DYyC8","tbWidth":"124","tbHeight":"113","unescapedUrl":"http://static.ddmcdn.com/gif/dog-best-friend-1.jpg","url":"http://static.ddmcdn.com/gif/dog-best-friend-1.jpg","visibleUrl":"animals.howstuffworks.com","title":"Is a \u003cb\u003edog\u003c/b\u003e really a man\u0026#39;s best friend?: \u003cb\u003eDog\u003c/b\u003e Care: Animal Planet","titleNoFormatting":"Is a dog really a man\u0026#39;s best friend?: Dog Care: Animal Planet","originalContextUrl":"http://animals.howstuffworks.com/pets/dog-best-friend.htm","content":"\u003cb\u003eDog\u003c/b\u003e Image Gallery","contentNoFormatting":"Dog Image Gallery","tbUrl":"http://t3.gstatic.com/images?q\u003dtbn:ANd9GcS6VxIzGltwNb2PWMGDfoBvvuof26Kn9i_BrXYRvgWqpnnT3UNfy5DYyC8"}],"cursor":{"resultCount":"3,460,000,000","pages":[{"start":"0","label":1},{"start":"4","label":2},{"start":"8","label":3},{"start":"12","label":4},{"start":"16","label":5},{"start":"20","label":6},{"start":"24","label":7},{"start":"28","label":8}],"estimatedResultCount":"3460000000","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/images?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003ddog","searchResultTime":"0.07"}}, "responseDetails": null, "responseStatus": 200} 

답변

0

대신 당신이처럼 URL을 수동으로 URL 인코딩을하는 :

NSString *google = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"string"]]; 
NSString *newGoogle = [google stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
NSString *fullGoogle = [NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%@",newGoogle]; 
NSURL * url = [NSURL URLWithString:fullGoogle]; 
NSURLRequest * request = [NSURLRequest requestWithURL:url 
              cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; 

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

방법을 시도해보십시오.

- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding 

은 URL의보다 정확한 인코딩을 수행합니다.

NSString *google = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"string"]]; 
NSString *fullGoogle = [NSString stringWithFormat:@"https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%@",google]; 
NSString * urlEncoded = [fullGoogle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL * url = [NSURL URLWithString:urlEncoded]; 
NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; 

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

아마도 이것이 여러분의 문제입니다.

+0

도움이되지 않습니다 , 나는 문제가 구글의 반환을 구문 분석하는 생각

NSError* error; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; NSDictionary *test = [json objectForKey:@"responseData"]; NSDictionary *closer = [test objectForKey:@"results"]; NSArray *closest = [closer valueForKey:@"url"]; 
Eric

0

당신은 connectionDidFinish이 필요