Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Google's freebase with POST method?
    text
    copied!<p>I'm having trouble using Google's freebase API in my java code. As it appears, my query is too large to send it in a GET method but I couldn't find a way to send it as a POST.</p> <p>This is the error I'm getting:</p> <pre><code> com.google.api.client.googleapis.json.GoogleJsonResponseException: 414 Request-URI Too Large &lt;!DOCTYPE html&gt; &lt;html lang=en&gt; &lt;meta charset=utf-8&gt; &lt;meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"&gt; &lt;title&gt;Error 414 (Request-URI Too Large)!!1&lt;/title&gt; &lt;style&gt; *{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}* &gt; 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}} &lt;/style&gt; &lt;a href=//www.google.com/&gt;&lt;img src=//www.google.com/images/errors/logo_sm.gif alt=Google&gt;&lt;/a&gt; &lt;p&gt;&lt;b&gt;414.&lt;/b&gt; &lt;ins&gt;That’s an error.&lt;/ins&gt; &lt;p&gt;The requested URL &lt;code&gt;/freebase/v1/mqlread&lt;/code&gt;... is too large to process. &lt;ins&gt;That’s all we know.&lt;/ins&gt; 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) </code></pre> <p>Here's the code:</p> <pre><code>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&lt;String&gt; 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(); } } } </code></pre> <p>I have also tried to change this part in Freebase class (line 450) by switching the <strong>HttpMethod.GET</strong> part into <strong>HttpMethod.POST</strong>, but I'm sure that it's not correct way:</p> <pre><code>/** * 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."); } </code></pre> <p>Is there a way of doing this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload