The first thing I see is the HTTP_REFERER being set to google.com. I would not be surprised if the API threw an error on that.
Jeremy R. Geerdes Generally Cool Guy Des Moines, IA For more information or a project quote: [email protected] If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church! On May 5, 2011, at 7:09 AM, sapna jain wrote: > Hello folks, > I wish to use google search API in my application made in procssing. > When I tried to run following code: > > import java.io.BufferedReader; > import java.io.InputStreamReader; > import java.net.URL; > import java.net.URLConnection; > import java.net.URLEncoder; > import org.json.JSONArray; // JSON library from http://www.json.org/java/ > import org.json.JSONObject; > > > // Put your website here > private final String HTTP_REFERER = "http://www.google.com/"; > > void setup() > { > size(700,700); > makeQuery("questio verum"); > //makeQuery("info:http://frankmccown.blogspot.com/"); > //makeQuery("site:frankmccown.blogspot.com"); > } > > void makeQuery(String query) { > > System.out.println(" Querying for " + query); > > try > { > // Convert spaces to +, etc. to make a valid URL > query = URLEncoder.encode(query, "UTF-8"); > // note: you can change the start index here. > URL url = new URL("http://ajax.googleapis.com/ajax/services/search/ > web?start=0&rsz=large&v=1.0&q=" + query+"&key=ABQIAAAAIyAHL0_ENlWOch- > kfrCVeRSsTL4WIgxhMZ0ZK_kHjwHeQuOD4xRWsfidvm2jtqgzKiXTEJnQWOxhnQ"); > println("hello before"); > > URLConnection connection = url.openConnection(); > connection.addRequestProperty("Referer", HTTP_REFERER); > > // Get the JSON response > String line; > StringBuilder builder = new StringBuilder(); > // println("hello"); > BufferedReader reader = new BufferedReader(new > InputStreamReader(connection.getInputStream())); > while((line = reader.readLine()) != null) { > builder.append(line); > } > > String response = builder.toString(); > JSONObject json = new JSONObject(response); > > System.out.println("Total results = " + > > json.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount")); > > JSONArray ja = > json.getJSONObject("responseData").getJSONArray("results"); > > System.out.println(" Results:"); > for (int i = 0; i < ja.length(); i++) { > System.out.print((i+1) + ". "); > JSONObject j = ja.getJSONObject(i); > System.out.println(j.getString("titleNoFormatting")); > System.out.println(j.getString("url")); > } > } > catch (Exception e) { > System.err.println("Something went wrong..."); > e.printStackTrace(); > } > } > > > java.net.connect exception was thrown.Complete description is as: > > Something went wrong... > java.net.ConnectException: Connection refused: connect > at java.net.PlainSocketImpl.socketConnect(Native Method) > at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) > at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java: > 195) > at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) > at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) > at java.net.Socket.connect(Socket.java:529) > at java.net.Socket.connect(Socket.java:478) > at sun.net.NetworkClient.doConnect(NetworkClient.java:163) > at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) > at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) > at sun.net.www.http.HttpClient.<init>(HttpClient.java:233) > at sun.net.www.http.HttpClient.New(HttpClient.java:306) > at sun.net.www.http.HttpClient.New(HttpClient.java:323) > at > sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java: > 860) > at > sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java: > 801) > at > sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java: > 726) > at > sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java: > 1049) > at sketch_may03a.makeQuery(sketch_may03a.java:67) > at sketch_may03a.setup(sketch_may03a.java:43) > at processing.core.PApplet.handleDraw(Unknown Source) > at processing.core.PApplet.run(Unknown Source) > at java.lang.Thread.run(Thread.java:619) > > > Can anyone here help me resolve the problem? > > -- > You received this message because you are subscribed to the Google Groups > "Google AJAX APIs" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-ajax-search-api?hl=en. > -- You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en.
