2016-12-11 20 views
0

私はxpagesからyandex Jsonインターフェイスapiへのjsonリクエストを作成するのに役立つ必要があります。このAPIのjson httpsリクエストを設定する方法 https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/#codesxpagesからyandexへのjsonリクエストの作成方法api

または

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

あなたはいくつかの良い答えが得られているようです。私は自分自身を追加するつもりですが、それは非常に区別できる答えではなく、私の瞬間(私のブログのもの)(https://edm00se.io/xpages/rest-consumption-server-side/)私の意見では少し更新する必要があります。 Stephan(stwissel)アプローチは、おそらく、彼が言及したような 'xe:restService'や他のもので実装するのが簡単だと思います。 –

答えて

3

最も簡単な方法のようにのXPagesで、このブロックを使用する方法

は、単純なJavaでそれをラップすることですクラスをパラメータなしのコンストラクタ(別名Bean)で呼び出すと、次のようなサーバー側のJavaScriptから呼び出されます。

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

コールバックのために私は残りのコントロールを使用することをお勧めしますので、それはあなたがこの例のような構造で、独自のカスタムRESTサービスを作成することができステファンの答えに加えて /yournsf.nsf/somepage.xsp/specialName

関連する問題