问题
The Microsoft Translator API for android samples has already been deprecated since march 2017 (just this yr). So I'm having a hard time translating text on android. Can anyone help me make this work on android?. It's just that I already have this working in java but I can't make it work on Android. I already tried using asynctask , but to no avail, no translation is being outputed. Or prolly I just did a wrong asyc task?.
The Interface of the application is like this:
It just have a simple textfield with translation that should be outputed on the other textfield. The translation is from Korean to English.
Github Project File is in here
https://github.com/iamjoshuabcxvii/MSTranslateAPIforAndroid
Android code is this.
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import org.apache.commons.io.IOUtils;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
//MS Translator Key is in here
public static String key = "<The MS Assigned Translator Key>";
private TextView txtTranslatedText, txtOriginalText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOriginalText = findViewById(R.id.txtOriginalText);
txtTranslatedText = findViewById(R.id.txtTranslatedText);
}
public void translate(View view) {
// String textOrig = txtOriginalText.getText().toString();
// String textOrig ="안녕하세요 123 -)^ 친구";
String textOrig;
textOrig=txtOriginalText.getText().toString();
String output;
output=getTranslation(textOrig);
txtTranslatedText.setText(output);
}
public static String getTranslation(String translatedTextStr) {
try {
// Get the access token
// The key got from Azure portal, please see https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account
String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
authConn.setRequestMethod("POST");
authConn.setDoOutput(true);
authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
IOUtils.write("", authConn.getOutputStream(), "UTF-8");
String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
// System.out.println(token); //Code to Display Token
// Using the access token to build the appid for the request url
String appId = URLEncoder.encode("Bearer " + token, "UTF-8");
String text = URLEncoder.encode(translatedTextStr, "UTF-8");
String from = "ko";
String to = "en";
String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/GetTranslations?appid=%s&text=%s&from=%s&to=%s&maxTranslations=5", appId, text, from, to);
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
translateConn.setRequestMethod("POST");
translateConn.setRequestProperty("Accept", "application/xml");
translateConn.setRequestProperty("Content-Type", "text/xml");
translateConn.setDoOutput(true);
String TranslationOptions = "<TranslateOptions xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">" +
"<Category>general</Category>" +
"<ContentType>text/plain</ContentType>" +
"<IncludeMultipleMTAlternatives>True</IncludeMultipleMTAlternatives>" +
"<ReservedFlags></ReservedFlags>" +
"<State>contact with each other</State>" +
"</TranslateOptions>";
translateConn.setRequestProperty("TranslationOptions", TranslationOptions);
IOUtils.write("", translateConn.getOutputStream(), "UTF-8");
String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
// System.out.println(resp+"\n\n");
String s=resp;
Pattern assign_op=Pattern.compile("(<TranslatedText>)"
+ "|(<\\/TranslatedText>)"
+ "|[()\\\\[\\\\]{};=#.,'\\\\^:@!$%&_`*-<>]"
+ "|[a-zA-Z0-9\\s]*"
+ "");
Matcher m = assign_op.matcher(s) ;
String actualTranslation="";
Boolean endOfTransTxt=false,startOfTransTxt=false,concat=false;
String foundRegexStr="",tempStr="";
while (m.find()) {
foundRegexStr=m.group();
if(m.group().matches("(<TranslatedText>)")) {
startOfTransTxt=true;
}
else if(m.group().matches("(<\\/TranslatedText>)")) {
endOfTransTxt=true;
concat=false;
}
else{
startOfTransTxt=false;
endOfTransTxt=false;
}
if(startOfTransTxt==true) {
concat=true;
}
else if(concat==true) {
tempStr=tempStr+""+m.group();
}
else {
}
}
// System.out.println("\nTranslated Text: "+tempStr);
translatedTextStr=tempStr;
} catch (Exception e) {
e.printStackTrace();
}
return translatedTextStr;
}
}
回答1:
I think you did a wrong AsyncTask
.
You can do it like this:
private class TransAsynTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
String output = getTranslation(strings[0]);
return output;
}
@Override
protected void onPostExecute(String s) {
txtTranslatedText.setText("Output: " + s);
super.onPostExecute(s);
}
}
And in the onCreate
you call:
String textOrig;
textOrig = txtOriginalText.getText().toString();
new TransAsynTask().execute(textOrig);
/*String output;
output = getTranslation(textOrig);
txtTranslatedText.setText("Translated Text: " + output);*/
来源:https://stackoverflow.com/questions/47499294/how-to-use-microsoft-translator-api-in-android