Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect to internet : Google Custom Search in java app "not connecting" ()
    primarykey
    data
    text
    <p>I am starting out on using google custom search api in java but it is just not connecting. I am receiving json data from google.(i am supposed to but it is not connecting..:() </p> <pre><code> HttpResponse response = httpClient.execute(httpGet); // exception is thrown at this point. </code></pre> <p>which is a line in my code. Code is : </p> <pre><code>public class MyGoogleSearch{ final static String searchURL = "https://www.googleapis.com/customsearch/v1?"; // This is Important : final static String apiKey = "My-Key-has a '-'"; final static String customSearchEngineKey = "My-Custom-search-engine-that-has a ':' too"; public String makeSearchString(String qSearch){ String toSearch = searchURL + "key=" + apiKey + "&amp;cx=" + customSearchEngineKey; toSearch += "&amp;q=" + qSearch + "&amp;alt=json"; return toSearch; } public static void main(String[] argv) throws IOException{ System.setProperty("proxyHost", "my-host"); System.setProperty("proxyPort","my-Port"); MyGoogleSearch browser = new MyGoogleSearch(); String toSearch = browser.makeSearchString("flower"); System.out.println(toSearch); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(toSearch); HttpResponse response = httpClient.execute(httpGet); // exception is thrown here. HttpEntity entity = response.getEntity(); if(entity != null){ InputStream inStream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); int length = 0; byte[] buffer = new byte[2048]; while((length = inStream.read(buffer)) != -1) System.out.println(buffer); String inputLine; while((inputLine = reader.readLine()) != null) System.out.println(inputLine); } } </code></pre> <p>}</p> <p>// Error is:</p> <pre><code>Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connection to https://www.googleapis.com refused at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158) </code></pre> <p>....</p> <pre><code>Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) </code></pre> <p>Is this the valid way to do ? I have not done any kind of UTF-8 encoding. Would that be a problem ? </p> <h2>On this Page:</h2> <p><a href="http://code.google.com/apis/customsearch/v1/getting_started.html" rel="nofollow">http://code.google.com/apis/customsearch/v1/getting_started.html</a><br> Here is an example of how this works in the JSON/Atom Custom Search API, which searches a test Custom Search Engine for lectures:</p> <p>GET <a href="https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&amp;cx=017576662512468239146:omuauf_lfve&amp;q=lectures" rel="nofollow">https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&amp;cx=017576662512468239146:omuauf_lfve&amp;q=lectures</a></p> <p>// Is this for javaScript only or for java too</p> <h2>I however put my Custom Search Engine Key at cx which should be right</h2> <p>I went to google api explorer <a href="https://code.google.com/apis/explorer/" rel="nofollow">https://code.google.com/apis/explorer/</a> </p> <p>I choose custom search and filled *q and cx column as i filled in my program:</p> <p>and it showed results and GET <a href="https://www.googleapis.com/customsearch/v1?q=o&amp;cx=" rel="nofollow">https://www.googleapis.com/customsearch/v1?q=o&amp;cx=</a>{MY-Custom-Search-Engine-No}&amp;pp=1&amp;key={YOUR_API_KEY}</p> <p><strong>but this time cx value had %3 at the place of ':' character.</strong></p> <p>How would i do that for all the characters in my keys and cse-no</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload