I am trying to create a java application that translates spanish into english. I am facing problem while translating spanish into english. But when i translate english into spansih it works. Here is my code. Here is my code. Can you please tell me my error. This code is working right now but when i change the values of fromLang to toLang from en to es it does not work.
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class GuiApp1 {
private static final String CLIENT_ID = "FREE_TRIAL_ACCOUNT";
private static final String CLIENT_SECRET = "PUBLIC_SECRET";
private static final String ENDPOINT = "http://api.whatsmate.net/v1/translation/translate";
public static void main(String[] args) throws Exception {
GuiApp1 g = new GuiApp1();
JFrame f=new JFrame();//creating instance of JFrame
f.setAlwaysOnTop( true );
JButton b=new JButton("Translate");//creating instance of JButton
b.setBounds(90,150,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
JTextArea t1,t2;
t1=new JTextArea(2,2);
String spanish;
t1.setBounds(50,100, 200,30);
t2=new JTextArea(2,2);
t2.setBounds(50,200, 200,30);
f.add(t1); f.add(t2);
f.setPreferredSize(new Dimension(200, 900));
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String text = t1.getText();
text = text.trim();
text = text.toLowerCase();
System.out.println(text);
String fromLang = "en";
String toLang = "es";
//String text = "Cuál es su nombre";
try{
translate(fromLang, toLang, text);
}catch(Exception e){
System.out.println(e);
}
}
});
}
/**
* Sends out a WhatsApp message via WhatsMate WA Gateway.
*/
public static void translate(String fromLang, String toLang, String text) throws Exception {
// TODO: Should have used a 3rd party library to make a JSON string from an object
String jsonPayload = new StringBuilder()
.append("{")
.append("\"fromLang\":\"")
.append(fromLang)
.append("\",")
.append("\"toLang\":\"")
.append(toLang)
.append("\",")
.append("\"text\":\"")
.append(text)
.append("\"")
.append("}")
.toString();
URL url = new URL(ENDPOINT);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("X-WM-CLIENT-ID", CLIENT_ID);
conn.setRequestProperty("X-WM-CLIENT-SECRET", CLIENT_SECRET);
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(jsonPayload.getBytes());
os.flush();
os.close();
int statusCode = conn.getResponseCode();
System.out.println("Status Code: " + statusCode);
BufferedReader br = new BufferedReader(new InputStreamReader(
(statusCode == 200) ? conn.getInputStream() : conn.getErrorStream()
));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
}
}