Example usage for org.json JSONUrl getStringFromURL

List of usage examples for org.json JSONUrl getStringFromURL

Introduction

In this page you can find the example usage for org.json JSONUrl getStringFromURL.

Prototype

public static JSONObject getStringFromURL(URL u) 

Source Link

Usage

From source file:com.pie.jotta.util.command.Translate.java

public void parse(IRCMessage m) {
    ArrayList<String> args = m.getMessageArgs();
    String toTranslate = "";
    for (String s : args) {
        toTranslate += "%20" + s;
    }/*from w ww .j av a  2 s.  c o m*/
    try {
        JSONObject jo = JSONUrl
                .getStringFromURL(new URL("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
                        + toTranslate + "&langpair=|en"));
        String responseDetails = jo.getString("responseDetails");
        if (responseDetails.equals("invalid text")) {
            help(m);
        } else {
            JSONObject response = jo.getJSONObject("responseData");
            if (response != null) {
                String translation = response.getString("translatedText");
                if (!translation.equals(toTranslate.replaceAll("%20", " "))) {
                    sendMessage(m.getSource(), m.getSender() + ": "
                            + jo.getJSONObject("responseData").getString("translatedText"));
                } else {
                    sendMessage(m.getSource(), m.getSender() + ": Unable to find a suitable translation.");
                }
            } else {
                help(m);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}