如何从xpages创建json请求到yandex翻译api

时间:2016-12-11 00:43:19

标签: javascript json api xpages yandex-api

我需要帮助创建从xpages到yandex Json接口api的json请求。如何为此api配置json https请求。 https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/#codes

如何在xpages中使用此块

 URL myURL = new URL(serviceURL);
 HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
 String userCredentials = "username:password";
 String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
 myURLConnection.setRequestProperty ("Authorization", basicAuth);
 myURLConnection.setRequestMethod("POST");
 myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
 myURLConnection.setRequestProperty("Content-Language", "en-US");
 myURLConnection.setUseCaches(false);
 myURLConnection.setDoInput(true);
 myURLConnection.setDoOutput(true); 

示例代码blok像这样

https://translate.yandex.net/api/v1.5/tr.json/translate ? 
key=<API key>
& text=<text to translate>
& lang=<translation direction>
& [format=<text format>]
& [options=<translation options>]
& [callback=<name of the callback function>]

2 个答案:

答案 0 :(得分:3)

最简单的方法是将它包装在一个带有参数自由构造函数(a.k.a a Bean)的简单Java类中,然后从服务器端JavaScript调用它,如:

var myYandex = new myYandexBean();
myYandex.callServer(somethingtoTranslate);

对于回调,我建议使用休息控件,所以它看起来像 /yournsf.nsf/somepage.xsp/specialName

答案 1 :(得分:1)

除了Stephan的回答,您还可以使用如下示例的结构创建自己的自定义REST服务:https://stash.openntf.org/projects/DOMEX/repos/dominoexplorer/browse/ODP/Code/Java/net/notesx/domex/rest/AllDatabasesService.java