在 iPhone 应用程序中进行 Google 搜索

发布于 2024-08-30 08:02:53 字数 140 浏览 5 评论 0原文

我想让用户在我的应用程序中输入关键字,然后在谷歌中搜索该关键字,对结果执行一些逻辑并向用户显示最终结论。

这可能吗?如何从我的应用程序执行 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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2024-09-06 08:02:53

对 Google AJAX 的 RESTful 搜索请求会在 JSON 格式。

您可以使用 ASIHTTPRequest 发出请求,并使用 json-framework

并提交基于 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 的示例,我还会向此 URL 添加一个 API 密钥。 Google 要求您使用 API 密钥进行搜索,但显然这不是必需的。您可以在此处注册 API 密钥。

还有异步请求方法,ASIHTTPRequest 文档中有详细介绍。您可以使用它们来防止 iPhone UI 在发出搜索请求时陷入困境。

一旦您掌握了 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]; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文