2013年3月14日木曜日

HttpClient4とMicrosoft Translator APIで翻訳する

前回の記事でAPIの呼び方を解説したが、その手順をJavaのプログラムにしてみた。 API呼び出しにはHttpClient4を使った。access_tokenの取得結果はJSONで返ってくるためその切り出しにはJSONICを使っている。

  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import net.arnx.jsonic.JSON;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  10. import org.apache.http.client.methods.HttpGet;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.impl.client.DefaultHttpClient;  
  13. import org.apache.http.message.BasicNameValuePair;  
  14. import org.apache.http.util.EntityUtils;  
  15.   
  16.   
  17. public class MSTranslatorTest {  
  18.   
  19.   public static void main(String[] args) {  
  20.     try {  
  21.       MSTranslatorTest app = new MSTranslatorTest();  
  22.       app.translate();  
  23.     } catch(Throwable ex) {  
  24.       ex.printStackTrace(System.err);  
  25.     }  
  26.   }  
  27.   
  28.   private void translate() throws IOException {  
  29.     DefaultHttpClient client = new DefaultHttpClient();  
  30.       
  31.     // Step1 access_tokenを取得  
  32.     // 取得のためのリクエストを準備  
  33.     HttpPost httpPost = new HttpPost("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");  
  34.     List<NameValuePair> params = new ArrayList<NameValuePair>();  
  35.     params.add(new BasicNameValuePair("grant_type""client_credentials"));  
  36.     // クライアント IDを設定  
  37.     params.add(new BasicNameValuePair("client_id""...."));  
  38.     // 顧客の秘密を設定  
  39.     params.add(new BasicNameValuePair("client_secret""...."));  
  40.     params.add(new BasicNameValuePair("scope""http://api.microsofttranslator.com"));  
  41.     httpPost.setEntity(new UrlEncodedFormEntity(params));  
  42.     // 取得実行  
  43.     String accessToken;  
  44.     HttpResponse response1 = client.execute(httpPost);  
  45.     try {  
  46.       // 結果はJSONとして返ってくるのでJSONICでaccess_tokenを切り出す  
  47.       HttpEntity entity = response1.getEntity();  
  48.       String reponseText = EntityUtils.toString(entity);  
  49.       Map json = JSON.decode(reponseText, Map.class);  
  50.       accessToken = (String)json.get("access_token");  
  51.     } finally {  
  52.       httpPost.releaseConnection();  
  53.     }  
  54.   
  55.     // Step2 翻訳する  
  56.     // 翻訳リクエストURLを作成  
  57.     // ・翻訳元=日本語 (from=ja)  
  58.     // ・翻訳先=スペイン語 (to=es)  
  59.     // ・翻訳する文字列 (text)  
  60.     String text = "こんにちは";  
  61.     String uri = String.format("http://api.microsofttranslator.com/V2/Http.svc/Translate?from=ja&to=es&text=%s", text);  
  62.     // access_tokenをヘッダに付与。"Bearer "を前につける。  
  63.     String authorization = String.format("Bearer %s", accessToken);  
  64.     HttpGet httpGet = new HttpGet(uri);  
  65.     httpGet.setHeader("Authorization", authorization);  
  66.     // 翻訳実行  
  67.     HttpResponse response2 = client.execute(httpGet);  
  68.     try {  
  69.       // 結果はXMLで返ってくる。そのままコンソールに表示。  
  70.       HttpEntity entity = response2.getEntity();  
  71.       String reponseText = EntityUtils.toString(entity);  
  72.       System.out.println(reponseText);  
  73.     } finally {  
  74.       httpGet.releaseConnection();  
  75.     }  
  76.   }  
  77. }  

実行するとコンソールに次のような結果が表示される。

  1. <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">¡Buenas noches!</string>  

関連記事

0 件のコメント:

コメントを投稿