簡體   English   中英

使用Google API Objective-C客戶端構造MQL查詢(免費庫)

[英]Using Google API Objective-C client to construct MQL queries (Freebase)

首先,我要說我是MQL,Freebase和Google API的新手。

我正在嘗試使用Google API Objective-C客戶端從Freebase獲得結果,但是如果沒有生成的類,我找不到使用API​​的任何示例或信息。

我已經找到此頁面http://code.google.com/p/google-api-objectivec-client/wiki/簡介

但是“使用沒有生成的類的API”部分沒有提供有關構建這些查詢的任何相關信息,並且所包含的示例都是生成的類。

到目前為止,我發現我需要首先使用RPC URL創建一個GTLService對象(我猜這是https://www.googleapis.com/freebase ),設置API版本(沙盒環境的v1sandbox) ,並設置API密鑰(在這種情況下為kGoogleAPIKey)。

GTLService * service = [[GTLService alloc] init];
    service.rpcURL = [NSURL URLWithString:@"https://www.googleapis.com/freebase"];
    service.apiVersion = @"v1sandbox";
    service.APIKey = kGoogleAPIKey;

完成! 太好了,沒問題。

下一部分是我遇到的問題。 我的問題是,如何使用Google API Objective-C客戶端構造一個MQL查詢以從Freebase檢索結果?

在上面的鏈接的“對象和查詢”部分中,它指出我需要創建一個查詢並執行它,但是在哪里包括MQL查詢?

// queryWithMethodName: methodName is the RPC method name to use
GTLQuery * query = [GTLQuery queryWithMethodName:@"mqlread"]; // Not sure if this is correct
GTLServiceTicket * ticket = [service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
    NSArray * items = [object items];
    NSLog(@"%@", [items description]);
    // Do something with items.
}];

作為參考,Freebase API URL是

https://www.googleapis.com/freebase/v1/mqlread?query={}

MQL查詢是

[{
"id":   null,
"name": null,
"type": "/travel/travel_destination",
"/travel/travel_destination/tourist_attractions": [{
   "id":   null,
   "name": null
}],
"/location/location/containedby": [{
   "name": "California"
}]
}]

我非常感謝您提供的任何幫助,甚至對正確方向的幫助!

我不確定為什么不能使用生成的類,但是當前的方法存在一些基本的誤解。

最重要的是,RPC API是與其他API(例如Freebase)不同的API端點。 我也很確定MQLRead與RPC API不兼容,因為它的返回結果沒有確定性的形狀。 如果兼容,則方法名稱將為freebase.mqlread,而不是mqlread。

如果由於某種原因您確實不能使用generate類,那么我會自己動手構造URL。 您需要做的就是將查詢作為JSON對象並將其序列化為查詢參數,然后添加其他任何參數,例如API密鑰。 結果也都是JSON,因此只需將它們反序列化為可以使用的JSON對象即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM