Package net.anwari

Source Code of net.anwari.TranslateString

package net.anwari;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.joda.time.LocalTime;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class TranslateString {

  public static void main(String[] args) throws MalformedURLException,
      IOException, JSONException {

    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);

    String query = "My name is Jahangir";
    String urlString = "http://mymemory.translated.net/api/get?q="
        + URLEncoder.encode(query, "UTF-8") + "&langpair=en|hi";

    URL url = new URL(urlString);

    BufferedReader reader = new BufferedReader(new InputStreamReader(
        url.openStream()));

    String line = null;
    String json = null;
    while ((line = reader.readLine()) != null) {
      json = line;
    }

    reader.close();

    JSONTokener tokener = new JSONTokener(json);
    JSONObject response = (JSONObject) tokener.nextValue();
    JSONObject translatedText = response.getJSONObject("responseData");

    // System.out.println(translatedText.getString("translatedText"));
    System.out.println(translatedText.get("translatedText"));
    System.out.println(response.getJSONObject("responseData").get(
        "translatedText"));

  }
}
TOP

Related Classes of net.anwari.TranslateString

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.