Back to project page GoogleTranslateAndroid.
The source code is released under:
Apache License
If you think the Android project GoogleTranslateAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ph.vainsolutions.googletranslatesampleandroid; //from w ww . ja v a 2 s . c o m import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLEncoder; import javax.net.ssl.HttpsURLConnection; /** * * @author Ravishanker Kusuma */ public class GoogleTranslateMainActivity { private String key; public GoogleTranslateMainActivity(String apiKey) { key = apiKey; } public GoogleTranslateMainActivity() { } String translte(String text, String from, String to) { StringBuilder result = new StringBuilder(); try { String encodedText = URLEncoder.encode(text, "UTF-8"); String urlStr = "https://www.googleapis.com/language/translate/v2?key=" + key + "&q=" + encodedText + "&target=" + to + "&source=" + from; URL url = new URL(urlStr); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); InputStream stream; if (conn.getResponseCode() == 200) //success { stream = conn.getInputStream(); } else stream = conn.getErrorStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String line; while ((line = reader.readLine()) != null) { result.append(line); } JsonParser parser = new JsonParser(); JsonElement element = parser.parse(result.toString()); if (element.isJsonObject()) { JsonObject obj = element.getAsJsonObject(); if (obj.get("error") == null) { String translatedText = obj.get("data").getAsJsonObject(). get("translations").getAsJsonArray(). get(0).getAsJsonObject(). get("translatedText").getAsString(); return translatedText; } } if (conn.getResponseCode() != 200) { System.err.println(result); } } catch (IOException | JsonSyntaxException ex) { System.err.println(ex.getMessage()); } return null; } public static void main(String[] args) { GoogleTranslateMainActivity translator = new GoogleTranslateMainActivity("AIzaSyDWFR8MyCDerdE4ZqEFmfyfA2HJ2EYNgFw"); String text = translator.translte("bahay", "tl", "en"); System.out.println(text); } }