簡體   English   中英

如何通過POST方法使用Google的freebase?

[英]How to use Google's freebase with POST method?

我在Java代碼中使用Google的freebase API時遇到問題。 看起來好像我的查詢太大,無法使用GET方法發送它,但是我找不到以POST形式發送它的方法。

這是我得到的錯誤:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 414 Request-URI Too Large
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 414 (Request-URI Too Large)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>414.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/freebase/v1/mqlread</code>... is too large to process.  <ins>That’s all we know.</ins>

    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:143)
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:187)
    at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:279)
    at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)
    at com.huawei.mdf.MQLReadService.doMqlRead(MQLReadService.java:67)
    at com.huawei.mdf.MQLReadService.main(MQLReadService.java:82)

這是代碼:

public void doMqlRead(String id, QueryType queryType) {

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest arg0) throws IOException {
            // TODO Auto-generated method stub

        }
    };

    JsonHttpRequestInitializer requestInitializer = new JsonHttpRequestInitializer() {
        public void initialize(JsonHttpRequest jsonHttpRequest)
                throws IOException {
            FreebaseRequest freebaseRequest = (FreebaseRequest) jsonHttpRequest;
            freebaseRequest.setPrettyPrint(true);
            freebaseRequest.setKey(API_KEY);
        }
    };

    Freebase.Builder fbb = new Freebase.Builder(httpTransport, jsonFactory,
            httpRequestInitializer);

    Freebase freebase = fbb.setJsonHttpRequestInitializer(
            requestInitializer).build();

    List<String> queryList = QueryUtil.getQuery(queryType);
    for (String query : queryList) {
        query = query.replace("idreplace", id);
        System.out.println(query.length());

        try {
            Mqlread mqlread = freebase.mqlread(query);
            mqlread.setIndent(indentation);

            HttpResponse executeUnparsed = mqlread.executeUnparsed();
            String parseAsString = executeUnparsed.parseAsString();
            System.out.println(parseAsString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我也試圖通過HttpMethod.GET部分轉換成HttpMethod.POST中的游離鹼類(線450)來改變這一部分,但我敢肯定,這不是正確的方法:

/**
     * Internal constructor. Use the convenience method instead.
     */
    Mqlread(String query) {
        super(Freebase.this, HttpMethod.GET, REST_PATH, null);
        this.query = Preconditions.checkNotNull(query,
                "Required parameter query must be specified.");
    }

有辦法嗎?

似乎沒有一種方法可以從Google API客戶端庫中覆蓋API調用的HTTP方法。 您必須像這樣自己構建請求對象:

HttpTransport httpTransport = new NetHttpTransport();
HttpRequestFactory httpRequestFactory = httpTransport.createRequestFactory();

String data = "query=" + query;
HttpContent content = new ByteArrayContent("application/x-www-form-urlencoded", data.getBytes());
GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");

HttpRequest request = httpRequestFactory.buildPostRequest(url, content);
HttpHeaders headers = new HttpHeaders();
headers.put("X-HTTP-Method-Override","GET");
request.setHeaders(headers);

HttpResponse response = request.execute();

請注意,我已經設置了X-HTTP-Method-Override標頭,以便在服務器端將此請求作為GET請求處理。

我已經提交了一個錯誤 ,供客戶端庫自動處理。

暫無
暫無

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

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