如何从Xpages创建JSON请求到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); 

示例代码像这样

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>]

最简单的方法是将其包裹在带有无参数构造函数(又称abe的简单Java类)中,然后从服务器端称呼它,例如:

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

对于回调,我建议使用REST控件,因此看起来像 /yournsf.nsf/somepage.xsp/specialName

除了斯蒂芬(Stephan)的答案外,您还可以创建自己的自定义休息服务,具有这样的结构:https://stash.openntf.org/projects/projects/domex/domex/repos/repos/repos/repos/dominoexplorer/browse/browse/odp/code/java/net/notesx/domex/rest/alldatabasesservice.java

相关内容

  • 没有找到相关文章

最新更新