Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the best and correct way is this</p> <pre><code>private static final String gurl = "www.googleapis.com"; private static final String gpath = "/language/translate/v2/detect"; public String detectLangGooglePost(String text) throws SystemException { List&lt;NameValuePair&gt; qparams = new ArrayList&lt;NameValuePair&gt;(); qparams.add(new BasicNameValuePair("key", key)); URI uri; try { uri = URIUtils.createURI("https", gurl, -1, gpath, URLEncodedUtils.format(qparams, "UTF-8"), null); } catch (URISyntaxException e) { throw new SystemException("Possibly invalid URI parameters", e); } HttpResponse response = getPostResponse(uri, text); StringBuilder builder = getBuilder(response); String language = getLanguage(builder); return language; } private HttpResponse getPostResponse(URI uri, String text) throws SystemException { List&lt;NameValuePair&gt; qparams = new ArrayList&lt;NameValuePair&gt;(); qparams.add(new BasicNameValuePair("q", text)); HttpResponse response; HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(uri); httpPost.addHeader("X-HTTP-Method-Override", "GET"); try { httpPost.setEntity(new UrlEncodedFormEntity(qparams)); response = httpclient.execute(httpPost); } catch (Exception e) { throw new SystemException("Problem when executing Google get request", e); } int sc = response.getStatusLine().getStatusCode(); if (sc != HttpStatus.SC_OK) throw new SystemException("google status code : " + sc); return response; } private StringBuilder getBuilder(HttpResponse response) throws SystemException { HttpEntity entity = response.getEntity(); if (entity == null) throw new SystemException("response entity null"); StringBuilder builder = new StringBuilder(); BufferedReader in = null; String str; try { in = new BufferedReader(new InputStreamReader(entity.getContent())); while ((str = in.readLine()) != null) builder.append(str); } catch (IOException e) { throw new SystemException("Reading input stream of http google response entity problem", e); } finally { IOUtils.closeQuietly(in); } if (builder.length() == 0) throw new SystemException("content stream of response entity empty has zero length"); return builder; } private String getLanguage(StringBuilder builder) throws SystemException { JSONObject data = null; JSONArray detections = null; String language = null; JSONObject object = (JSONObject) JSONValue.parse(builder.toString()); if (object == null) throw new SystemException("JSON parsing builder object returned null"); if (object.containsKey("data") == false) throw new SystemException("JSONObject doesn't contain data key"); data = (JSONObject) object.get("data"); detections = (JSONArray) data.get("detections"); if (detections == null) throw new SystemException("JSON detections is null"); JSONObject body = (JSONObject) ((JSONArray) detections.get(0)).get(0); if (body == null) throw new SystemException("detections body is null"); if (body.containsKey("language") == false) throw new SystemException("language key is null"); language = (String) body.get("language"); if (language == null || language.equals(unknown)) throw new SystemException("Google lang detection - resulting language : " + language); return language; } </code></pre>
 

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