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..:()
HttpResponse response = httpClient.execute(httpGet);
// exception is thrown at this point.
which is a line in my code. Code is :
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 + "&cx=" + customSearchEngineKey;
toSearch += "&q=" + qSearch + "&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);
}
}
}
// Error is:
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)
....
Caused by: java.net.ConnectException: Connection time开发者_运维知识库d out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
Is this the valid way to do ? I have not done any kind of UTF-8 encoding. Would that be a problem ?
On this Page:
http://code.google.com/apis/customsearch/v1/getting_started.html
Here is an example of how this works in the JSON/Atom Custom Search API, which searches a test Custom Search Engine for lectures:GET https://www.googleapis.com/customsearch/v1?key=INSERT-YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=lectures
// Is this for javaScript only or for java too
I however put my Custom Search Engine Key at cx which should be right
I went to google api explorer https://code.google.com/apis/explorer/
I choose custom search and filled *q and cx column as i filled in my program:
and it showed results and GET https://www.googleapis.com/customsearch/v1?q=o&cx={MY-Custom-Search-Engine-No}&pp=1&key={YOUR_API_KEY}
but this time cx value had %3 at the place of ':' character.
How would i do that for all the characters in my keys and cse-no
the problem seems to be connecting to googleapis, are you sure you're using the correct proxy? Try just connecting to google.com or any URL to test you are getting past the proxy, try it without setting the proxy as well.
精彩评论