It is a program to
obtain meaning of words which are redirected to google as parameters.
The results are fetched and printed using json object in java.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import javax.swing.JOptionPane;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Dictonary
{
public static void main(String args[]) throws Exception
{
String str = null;
System.out.println("running");;
str = JOptionPane.showInputDialog("Enter the string");
System.setProperty("http.proxyHost", "192.168.0.17");
System.setProperty("http.proxyPort", "8080");
JSONObject json = readJsonFromUrl("http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q="+str+"&sl=en&tl=en&restrict=pr%2Cde&client=te");
JSONArray json1 = json.getJSONArray("webDefinitions");
JSONArray json2 = ((JSONObject)json1.get(0)).getJSONArray("entries");
System.out.println("MEANINGS OF "+str);
for(int i=0;i<json2.length();i++)
{
JSONArray jobarray =((JSONObject)json2.get(i)).getJSONArray("terms");
for(int j=0;j<jobarray.length();j++)
{
JSONObject job = jobarray.getJSONObject(j);
String stri = (String)job.get("type");
if(stri.equals("text"))
{
System.out.println(job.get("text"));
}
}
System.out.println("");
}
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException
{
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
// System.out.println("char 1 "+jsonText.trim());
jsonText= jsonText.substring(jsonText.indexOf('(')+1,jsonText.lastIndexOf(')'));
// System.out.println("char 2 "+jsonText.trim());
JSONObject json = new JSONObject(jsonText.trim());
return json;
} finally {
is.close();
}
}
private static String readAll(Reader rd) throws IOException
{
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}