从iPhone应用程序中进行Google搜索(Google search from within an iPhone app)

编程入门 行业动态 更新时间:2024-10-28 06:29:51
从iPhone应用程序中进行Google搜索(Google search from within an iPhone app)

我希望让用户在我的应用中输入关键字,然后在Google上搜索此关键字,对结果执行一些逻辑并向用户显示最终结论。

这可能吗? 如何从我的应用程序执行谷歌搜索? 答复的格式是什么? 如果有人有这样的代码示例,他们将不胜感激。

谢谢,

I want to have the user enter a keyword in my app and then search google for this keyword, perform some logic on the results and display a final conclusion to the user.

Is this possible? How do I perform the search on google from my app? What is the format of the reply? If anybody has some code samples for this, they would be greatly appreciated.

Thanks,

最满意答案

Google AJAX的RESTful搜索请求会返回JSON格式的响应。

您可以使用ASIHTTPRequest发出请求,并使用json-framework在iPhone上解析JSON格式的响应。

例如,要创建和提交基于Google AJAX页面上的示例的搜索请求,您可以使用ASIHTTPRequest的-requestWithURL和-startSynchronous方法:

NSURL *searchURL = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton"]; ASIHTTPRequest *googleRequest = [ASIHTTPRequest requestWithURL:searchURL]; [googleRequest addRequestHeader:@"Referer" value:[self deviceIPAddress]]; [googleRequest startSynchronous];

您将根据搜索条件构建NSURL实例, 转义请求参数。

如果我按照Google的示例,我也会在此网址中添加API密钥。 Google要求您使用API​​密钥进行搜索,但显然这不是必需的。 您可以在这里注册一个API密钥。

还有ASIHTTPRequest文档中详细介绍的异步请求方法。 您可以使用这些功能来防止在执行搜索请求时iPhone绑定。

一旦获得Google的JSON格式的响应,您可以使用json-framework SBJSON解析器对象将响应解析为NSDictionary对象:

NSError *requestError = [googleRequest error]; if (!requestError) { SBJSON *jsonParser = [[SBJSON alloc] init]; NSString *googleResponse = [googleRequest responseString]; NSDictionary *searchResults = [jsonParser objectWithString:googleResponse error:nil]; [jsonParser release]; }

您还应该在请求头中指定引用者IP地址,在这种情况下,它将是iPhone的本地IP地址,例如:

- (NSString *) deviceIPAddress { char iphoneIP[255]; strcpy(iphoneIP,"127.0.0.1"); // if everything fails NSHost *myHost = [NSHost currentHost]; if (myHost) { NSString *address = [myHost address]; if (address) strcpy(iphoneIP, [address cStringUsingEncoding:NSUTF8StringEncoding]); } return [NSString stringWithFormat:@"%s",iphoneIP]; }

A RESTful search request to Google AJAX returns a response in JSON format.

You can issue the request with ASIHTTPRequest and parse the JSON-formatted response on an iPhone with json-framework.

For example, to create and submit a search request that is based on the example on the Google AJAX page, you could use ASIHTTPRequest's -requestWithURL and -startSynchronous methods:

NSURL *searchURL = [NSURL URLWithString:@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton"]; ASIHTTPRequest *googleRequest = [ASIHTTPRequest requestWithURL:searchURL]; [googleRequest addRequestHeader:@"Referer" value:[self deviceIPAddress]]; [googleRequest startSynchronous];

You would build the NSURL instance based on your search terms, escaping the request parameters.

If I followed Google's example to the letter, I would also add an API key to this URL. Google asks that you use an API key for searches, but apparently it is not required. You can sign up for an API key here.

There are also asynchronous request methods which are detailed in the ASIHTTPRequest documentation. You would use those to keep the iPhone UI from getting tied up while the search request is made.

Once you have Google's JSON-formatted response in hand, you can use the json-framework SBJSON parser object to parse the response into an NSDictionary object:

NSError *requestError = [googleRequest error]; if (!requestError) { SBJSON *jsonParser = [[SBJSON alloc] init]; NSString *googleResponse = [googleRequest responseString]; NSDictionary *searchResults = [jsonParser objectWithString:googleResponse error:nil]; [jsonParser release]; }

You should also specify the referer IP address in the request header, which in this case would be the local IP address of the iPhone, e.g.:

- (NSString *) deviceIPAddress { char iphoneIP[255]; strcpy(iphoneIP,"127.0.0.1"); // if everything fails NSHost *myHost = [NSHost currentHost]; if (myHost) { NSString *address = [myHost address]; if (address) strcpy(iphoneIP, [address cStringUsingEncoding:NSUTF8StringEncoding]); } return [NSString stringWithFormat:@"%s",iphoneIP]; }

更多推荐

本文发布于:2023-04-28 00:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   iPhone   Google   app   search

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!