I made an Android app that translates using the MicrosoftTranslatorText API, so I will keep a memorandum of the introduction method. In this article Translate using Microsoft Translator Text API on Android ~ Preparation ~ Using the key obtained in, we will implement the translation function in the Android application with the MicrosofTranslatorText API.
http://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate
To translate using the MicrosoftTranslatorTextAPI, you need to get an API token first. Create a class that inherits AsyncTask because it communicates.
TranslateAPIGetTokenRequest.java
package <package name>;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class TranslateAPIGetTokenRequest extends AsyncTask<String, Void, String>{
private static final String KEY = "<API key obtained in preparation>"; //API key
private static final String LOG_TAG = "GetTokenRequest"; //Log tag
private static final String HTTP_OK = "HTTP_OK"; //HttpUrlConnection status
private String mTranslateWord = ""; //Translated text
private String mApiToken = ""; //API token
private MainActivity mActivity; //For callback
//constructor
public TranslateAPIGetTokenRequest(MainActivity activity,String word){
mActivity = activity;
mTranslateWord = word;
}
@Override
protected String doInBackground(String... params) {
String urlStr = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; //API token acquisition request URL
String result = null;
HttpURLConnection con;
URL url = null;
BufferedReader reader = null;
try{
url = new URL(urlStr);
}catch(Exception e){
e.printStackTrace();
}
try {
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.addRequestProperty("Content-Type","application/json");
con.addRequestProperty("Ocp-Apim-Subscription-Key", KEY);
final int status = con.getResponseCode();
Log.d(LOG_TAG, "result:" + status);
if (status == HttpURLConnection.HTTP_OK) {
result= HTTP_OK;
StringBuilder stringBuilder = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
String recvData = stringBuilder.toString();
mApiToken = recvData;
Log.d(LOG_TAG, "http post success recvData: " + recvData);
} else{
result = String.valueOf(status);
}
}catch(Exception e){
Log.e(LOG_TAG,e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result) {
Log.d(LOG_TAG, "onPostExecute, result: " + result);
//Status is HTTP_If it is OK, start a translation request
if(result.equals(HTTP_OK)) {
TranslateAPITranslateRequest trans = new TranslateAPITranslateRequest(mActivity, mTranslateWord, mApiToken);
trans.execute();
}else{
Log.d("GetTokenRequest", "onPostExecute:Error");
}
}
}
private static final String KEY = "
This is because I want to specify the text to translate when calling from Activity.
//constructor
public TranslateAPIGetTokenRequest(MainActivity activity,String word){
mActivity = activity;
mTranslateWord = word;
}
The endpoint of API token acquisition is as follows. https://api.cognitive.microsoft.com/sts/v1.0/issueToken
String urlStr = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; //API token acquisition request URL
You need to set the API key in the parameter Ocp-Apim-Subscription-Key.
Request method: POST Content-Type:application/json ** Ocp-Apim-Subscription-Key: API key ** (key obtained in preparation)
con.setRequestMethod("POST");
con.addRequestProperty("Content-Type","application/json");
con.addRequestProperty("Ocp-Apim-Subscription-Key", KEY);
If the request is successful, a string of about 700 characters will be returned. That is the API token.
if (status == HttpURLConnection.HTTP_OK) {
result= HTTP_OK;
StringBuilder stringBuilder = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
String recvData = stringBuilder.toString();
mTranslateToken = recvData;
Log.d(LOG_TAG, "http post success recvData: " + recvData);
} else {
result = String.valueOf(status);
}
At this time, specify the API token, callback destination activity, and translated text obtained in 5 in the argument of the constructor.
@Override
protected void onPostExecute(String result) {
Log.d(LOG_TAG, "onPostExecute, result: " + result);
//Status is HTTP_If it is OK, start a translation request
if(result.equals(HTTP_OK)) {
TranslateAPITranslateRequest trans = new TranslateAPITranslateRequest(mActivity, mTranslateWord, mTranslateToken);
trans.execute();
}else{
Log.d("GetTokenRequest", "onPostExecute:Error");
}
}
It seems nonsense to call the AsyncTask class in succession like this ...
Make a translation request using the API token obtained in 1. As with the API token acquisition request, use a class that inherits AsyncTask.
TranslateAPITranslateRequest.java
package <package name>;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class TranslateAPITranslateRequest extends AsyncTask<String, Void, String> {
private static final String LOG_TAG = "TranslateRequest"; //Log tag
private static final String HTTP_OK = "HTTP_OK";
private String mTranslateWord = "";//Untranslated text
private String mTranslatedWord = ""; //Post-translation text
private String mApiToken = ""; //API token
private MainActivity mActivity; //Callback destination Activity
//constructor
public TranslateAPITranslateRequest(MainActivity activity,String word,String token){
Log.d(LOG_TAG,"word:" + word);
mActivity = activity;
mTranslateWord = word;
mApiToken = token;
}
@Override
protected String doInBackground(String... params) {
Log.d(LOG_TAG,"doInBackground,translateWord:" + mTranslateWord);
String urlStr = null;
try {
urlStr = "http://api.microsofttranslator.com/v2/Http.svc/Translate?from=ja&to=en&text=" + URLEncoder.encode(mTranslateWord, "UTF-8") ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = null;
HttpURLConnection con;
URL url = null;
BufferedReader reader = null;
try{
url = new URL(urlStr);
}catch(Exception e){
e.printStackTrace();
}
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("Authorization","Bearer " + mApiToken);
final int status = con.getResponseCode();
Log.d(LOG_TAG, "result:" + status);
if (status == HttpURLConnection.HTTP_OK) {
//Processing to receive response, etc.
result=HTTP_OK;
StringBuilder stringBuilder = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
stringBuilder.append(inputLine);
}
mTranslatedWord = stringBuilder.toString();
mTranslatedWord = mTranslatedWord.replace("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">", "");
mTranslatedWord = mTranslatedWord.replace("</string>", "");
Log.d(LOG_TAG, "http post success recvData: " + mTranslatedWord);
} else{
result=String.valueOf(status);
}
}catch(Exception e){
Log.e(LOG_TAG, e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result) {
Log.d(LOG_TAG, "onPostExecute, result:" + result);
if(result.equals(HTTP_OK)) {
mActivity.callback(MainActivity.CALLBACK_FINISH_TRANSLATE_CODE, mTranslatedWord);
}else{
Log.d(LOG_TAG, "onPostExecute:Error");
}
}
}
//constructor
public TranslateAPITranslateRequest(MainActivity activity,String word,String token){
Log.d(LOG_TAG,"word:" + word);
mActivity = activity;
mTranslateWord = word;
mApiToken = token;
}
The translation request endpoints are: http://api.microsofttranslator.com/v2/Http.svc/Translate The URL parameters are as follows. text: The text you want to translate. ** Required ** to: Translated language code. ** Required ** from: Language code before translation. Any appid: API token. ** Required, but not required if header parameter: Authorization specifies API token. ** **
try {
urlStr = "http://api.microsofttranslator.com/v2/Http.svc/Translate?from=ja&to=en&text=" + URLEncoder.encode(mTranslateWord, "UTF-8") ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
This time, we will translate from Japanese to English, so we have specified from = ja and to = en. The API token is then specified in the header parameter.
Request method: GET (There is a theory that there is no problem with POST) Authorization: API token (*)
The following is the format of the API token. ** "Bearer \ <API token >" ** You need to put a Bearer on the head. ** Please note that there is a space between the Bearer and the API token. ** ** This format is also used when the appid is specified in the URL parameter.
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("Authorization","Bearer " + mApiToken);
If the request is successful, the translation result will be returned in the following format.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">[Translation result]</string>
I want to return only the translation result, so delete the unnecessary part.
mTranslatedWord = stringBuilder.toString();
mTranslatedWord = mTranslatedWord.replace("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">", "");
mTranslatedWord = mTranslatedWord.replace("</string>", "");
Quite forcible?
If the communication is successful, the received translation result will be called back to Activity. I am using the callback function implemented in MainActivity.
@Override
protected void onPostExecute(String result) {
Log.d(LOG_TAG, "onPostExecute, result:" + result);
if(result.equals(HTTP_OK)) {
mActivity.callback(MainActivity.CALLBACK_FINISH_TRANSLATE_CODE, mTranslatedWord);
}else{
Log.d(LOG_TAG, "onPostExecute:Error");
}
}
After that, it is OK if you display the translation result on TextView etc. on the Activity side.
MainActivity.java
/**
*Callback method.
*Called after background processing is completed.
*/
public void callback(final int responseCode, String word) {
if (CALLBACK_FINISH_TRANSLATE_CODE == responseCode) {
translateResultText.setText(word);
}
}
When starting translation, describe as follows in Activity and start from acquisition of API token.
TranslateAPIGetTokenRequest trans = new TranslateAPIGetTokenRequest(MainActivity.this, [The text you want to translate]);
trans.execute();
I tried to translate the character string entered in EditText to see how accurate it actually is. The text is pulled from the official documentation of the Microsoft Translator API.
In this way, the English text of the translated word is completely different.
As expected, Google seems to have higher translation accuracy. But I wish I could do this for free!
That's it.
Recommended Posts