Google图片搜索iPhone网址数据

时间:2012-02-21 15:56:11

标签: iphone nsurlconnection google-api

我想做一个简单的谷歌图片搜索,并为我正在搜索的字符串返回一个图像。当我完成时,图像为空,我做错了什么?代码如下:

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

当我搜索狗时,我会在网络浏览器中找到它:

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

2 个答案:

答案 0 :(得分:0)

而不是像您一样对您的网址进行手动网址编码:

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

可以更准确地编码网址:

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

也许这是你的问题,希望它有所帮助!

答案 1 :(得分:0)

您需要在connectionDidFinish

中使用此功能
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"];
相关问题