访问 Google 翻译器 API
Accessing Google Translator API
我正在使用 google 翻译器 API 来翻译给定的文本。通过下拉菜单选择语言,并将其值发送到 servlet。通过servlet 翻译方法被调用。但随后出现以下错误。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Reque
st
{
"code": 400,
"errors": [
{
"domain": "global",
"message": "Invalid Value",
"reason": "invalid"
}
],
"message": "Invalid Value"
}
dropdown.jsp
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>" > <%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = "<%=name%>"/>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
<a href="#search" class="btn btn-default bg-light-gray">Search Text</a>
</div>
</div>
</form>
textTranslation.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession extractedText = request.getSession();
String imageName = (String) extractedText.getAttribute("text");
System.out.println("This is "+imageName);
String name = request.getParameter("country");
if(name == null)
{
System.out.println("null");
}
System.out.println("I selected " +name);
//String name = "it";
TranslatorApi translation = new TranslatorApi();
String outputText = translation.getTranslate(imageName, name);
System.out.println("Second"+outputText);
request.setAttribute("outputText", outputText);
RequestDispatcher sndValue = request.getRequestDispatcher("/translation.jsp");
sndValue.forward(request, response);
}
TranslatorApi.java
private String apiKey = "AIzaSyCX2O-pteDLJLeMivT47kD9pucEv67QECQ";
public String getTranslate(String textToBeTranslated, String translateLang)
{
Translate translte = null;
String result = null;
try
{
try {
translte = new Translate.Builder(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(), com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
.setApplicationName("OCRProject")
.build();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(GeneralSecurityException ex)
{
ex.printStackTrace();
}
Translate.Translations.List list = null;
try {
list = translte.new Translations().list(Arrays.asList(textToBeTranslated),translateLang);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.setKey(apiKey);
TranslationsListResponse response = null;
try {
response = list.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (TranslationsResource tr : response.getTranslations()) {
result = tr.getTranslatedText();
}
return result;
}
但是如果我将代码值硬编码为
String name = "ES";
它给出了翻译后的文本。当我检查下拉菜单传递的值时,它不为空。它的代码值正确打印在控制台中。有人知道吗?提前谢谢你
在 TranslatorApi class 中添加 trim() 以删除不必要的空格。
public String getTranslate(String textToBeTranslated, String translateLang)
{
translateLang = translateLang.trim();
}
我正在使用 google 翻译器 API 来翻译给定的文本。通过下拉菜单选择语言,并将其值发送到 servlet。通过servlet 翻译方法被调用。但随后出现以下错误。
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Reque
st
{
"code": 400,
"errors": [
{
"domain": "global",
"message": "Invalid Value",
"reason": "invalid"
}
],
"message": "Invalid Value"
}
dropdown.jsp
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>" > <%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = "<%=name%>"/>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
<a href="#search" class="btn btn-default bg-light-gray">Search Text</a>
</div>
</div>
</form>
textTranslation.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession extractedText = request.getSession();
String imageName = (String) extractedText.getAttribute("text");
System.out.println("This is "+imageName);
String name = request.getParameter("country");
if(name == null)
{
System.out.println("null");
}
System.out.println("I selected " +name);
//String name = "it";
TranslatorApi translation = new TranslatorApi();
String outputText = translation.getTranslate(imageName, name);
System.out.println("Second"+outputText);
request.setAttribute("outputText", outputText);
RequestDispatcher sndValue = request.getRequestDispatcher("/translation.jsp");
sndValue.forward(request, response);
}
TranslatorApi.java
private String apiKey = "AIzaSyCX2O-pteDLJLeMivT47kD9pucEv67QECQ";
public String getTranslate(String textToBeTranslated, String translateLang)
{
Translate translte = null;
String result = null;
try
{
try {
translte = new Translate.Builder(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(), com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
.setApplicationName("OCRProject")
.build();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(GeneralSecurityException ex)
{
ex.printStackTrace();
}
Translate.Translations.List list = null;
try {
list = translte.new Translations().list(Arrays.asList(textToBeTranslated),translateLang);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.setKey(apiKey);
TranslationsListResponse response = null;
try {
response = list.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (TranslationsResource tr : response.getTranslations()) {
result = tr.getTranslatedText();
}
return result;
}
但是如果我将代码值硬编码为
String name = "ES";
它给出了翻译后的文本。当我检查下拉菜单传递的值时,它不为空。它的代码值正确打印在控制台中。有人知道吗?提前谢谢你
在 TranslatorApi class 中添加 trim() 以删除不必要的空格。
public String getTranslate(String textToBeTranslated, String translateLang)
{
translateLang = translateLang.trim();
}